gpt4 book ai didi

java - 多个数据输入/输出流混淆

转载 作者:可可西里 更新时间:2023-11-01 02:49:26 24 4
gpt4 key购买 nike

我正在创建一个允许上传和下载到文件服务器的简单程序。我需要在服务器完全在线之前在上传和下载之间做出选择。问题是,我对数据输入/输出流等感到非常困惑,目前上传选项不起作用。它会自行运行,但不会按照我在此处编写的方式运行。在这个阶段它让我头疼!

服务器代码:

import java.io.*;
import java.net.*;

import javax.swing.JOptionPane;

public class Server_Test {

public static void main(String a[]) throws Exception {

int timeoutsecs = 600;
int port = 4444;
Socket sock;
ServerSocket servsock = new ServerSocket(port, timeoutsecs);

while (true) {

// wait for the next client connection
sock = servsock.accept();

DataInputStream choice = new DataInputStream(sock.getInputStream());


if (choice.read() == 1){

// ****1*****
// Download
// Send I/O streams to the socket



DataOutputStream out = new DataOutputStream(sock.getOutputStream());
new Server_Test().sendFile(out);

}else if (choice.read() == 2){

// ****2****
//Upload
// Receive I/O streams from socket



DataInputStream in = new DataInputStream(sock.getInputStream());
new Server_Test().receiveFile(in);

}

// Close this connection, (not the overall // server socket)

sock.close();

}
}

public void sendFile(DataOutputStream os) throws Exception{

// String fileLoc = JOptionPane.showInputDialog(null, "What is the file location of the file you want to download?");
File file = new File("C:\\TestFile.txt");
FileInputStream fis = new FileInputStream(file);
byte [] mybytearray = new byte [(int)file.length()+1];

BufferedInputStream bis = new BufferedInputStream(fis);

bis.read(mybytearray,0,mybytearray.length);
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();

bis.close();


}

public void receiveFile(DataInputStream in) throws Exception{

int filesize=6022386;
int bytesRead;
int current = 0;
byte [] mybytearray = new byte [filesize];

FileOutputStream fos = new FileOutputStream("C:\\UploaTest.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = in.read(mybytearray,0,mybytearray.length);
current = bytesRead;


do {
bytesRead =
in.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bos.write(mybytearray, 0 , current);
bos.flush();
bos.close();

}

客户端代码:

import java.io.*;
import java.net.*;

import javax.swing.JOptionPane;

public class Client_Test {
public static void main(String a[]) throws Exception {

Socket sock;


// ServerSocket serverSocket = new ServerSocket(5556);

sock = new Socket("127.0.0.1", 4444);
System.out.println("Connecting...");

Integer choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Please type 1 for download and 2 for upload."));
DataOutputStream outs = new DataOutputStream(sock.getOutputStream());



outs.write(choice);
//outs.flush();
//outs.close();

if(choice == 1){

// *****1*****
//Download
// Get I/O streams from the socket
DataInputStream is = new DataInputStream(sock.getInputStream());
// receive file
new Client_Test().downloadFile(is);

}else if(choice == 2){

//*****2****
//Upload
//Send I/O streams to the socket.
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
new Client_Test().uploadFile(out);

}


sock.close();
}

public void downloadFile(DataInputStream in) throws Exception{

int filesize=6022386;
int bytesRead;
int current = 0;
byte [] mybytearray = new byte [filesize];

FileOutputStream fos = new FileOutputStream("C:\\DowloadTest.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = in.read(mybytearray,0,mybytearray.length);
current = bytesRead;


do {
bytesRead =
in.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bos.write(mybytearray, 0 , current);
bos.flush();
bos.close();


}



public void uploadFile (DataOutputStream out)throws Exception{

File file = new File("C:\\TestFile.txt");
FileInputStream fis = new FileInputStream(file);
byte [] mybytearray = new byte [(int)file.length()+1];

BufferedInputStream bis = new BufferedInputStream(fis);

bis.read(mybytearray,0,mybytearray.length);
System.out.println("Sending...");
out.write(mybytearray,0,mybytearray.length);
out.flush();


bis.close();

}

}

最佳答案

您遇到的问题是由这些线路引起的:

if (choice.read() == 1){

  }else if (choice.read() == 2){

流阻塞直到数据可用,所以服务器在第一个 if 行等待。

当客户端随后发送 1 或 2 时,读取调用继续并将读取的值与 1 进行比较。如果您发送值 2,则比较为 false,服务器继续到第二行并阻塞等待更多输入。

您应该将两个读取调用替换为一个:

int choiceValue = choice.read();
if (choiceValue==1)
{
}
else if (choiceValue==2
{
}

这应该可以解决您当前的问题。

我认为您的 Server_Test.receiveFile() 方法中可能存在另一个问题,因为看起来您正在尝试读取文件两次,一次是在这一行中

bytesRead = in.read(mybytearray,0,mybytearray.length);

然后再次循环:

do {
bytesRead =
in.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

关于java - 多个数据输入/输出流混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com