gpt4 book ai didi

java - 文件传输后Java Socket自动关闭

转载 作者:行者123 更新时间:2023-12-03 12:07:11 25 4
gpt4 key购买 nike

我正在尝试使用Java和套接字传输文件。我的问题是,在成功传输文件后,套接字在服务器端(我在其中传输文件)自身关闭了。
这是我尝试发送文件的服务器

public void send_file_to_client(String requested_file) throws IOException {
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
try {

File FILE_TO_SEND = new File("C:\\ServerMusicStorage\\" + requested_file + ".wav");
byte[] mybytearray = new byte[(int) FILE_TO_SEND.length()];

fis = new FileInputStream(FILE_TO_SEND);
bis = new BufferedInputStream(fis);

bis.read(mybytearray, 0, mybytearray.length);
os = connsock.getOutputStream();
System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + "bytes)");
toClient.writeUTF(Integer.toString(mybytearray.length));
os.write(mybytearray, 0, mybytearray.length);
os.flush();
System.out.println("Done.");
} finally {
if (bis != null) {
bis.close();
}
if (os != null) {
os.close();
}
}
}
这是我收到文件的客户端
public static void receive_file(String requested_file) throws IOException {
FileOutputStream fos = null;
BufferedOutputStream bos = null;

try {

File file_to_save = new File("C:\\ClientMusicStorage\\" + requested_file + ".wav");
int bytesRead;

String fileSize = fromServer.readUTF();
int final_file_size = Integer.parseInt(fileSize);

byte[] mybytearray = new byte[final_file_size];
InputStream is = newclientSocket.getInputStream();
fos = new FileOutputStream(file_to_save);
bos = new BufferedOutputStream(fos);
// bytesRead = is.read(mybytearray, 0, mybytearray.length);
// current = bytesRead;

while ((bytesRead = is.read(mybytearray)) > 0) {
bos.write(mybytearray, 0, bytesRead);
}
bos.flush();
System.out.println("File " + requested_file + ".wav" + " downloaded (" + final_file_size + " bytes read)");
} catch (EOFException e) {
//eof - no error in this case
} catch (IOException e) {
//something went wrong
e.printStackTrace();
} finally {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
}
}
这是文件传输后我得到的
SEVERE: null
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.net.SocketInputStream.read(SocketInputStream.java:223)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
怎么了?

最佳答案

基本上是因为一旦完成传输,您就关闭了输出流(finally块的一部分)。但是客户端代码仍在尝试从流中读取数据,请参阅代码的以下部分

 while ((bytesRead = is.read(mybytearray)) > 0) 
因此,如果您解决了这一部分,就不会遇到这个问题。基本上,可以从服务器将文件的大小发送给客户端,然后再发送文件的内容。然后在客户端,您可以先读取文件大小,然后读取下一个字节,只需将while循环替换为下一行。
int fileReadBytes = is.read(mybytearray, 0, final_file_size);     

关于java - 文件传输后Java Socket自动关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65706916/

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