gpt4 book ai didi

java - 为什么 .readUTF() over socket 会因小文件而失败?

转载 作者:行者123 更新时间:2023-11-29 09:06:42 25 4
gpt4 key购买 nike

我正在通过套接字将文件从客户端传输到基本服务器。当我传输小文件 (1kb) 时,它在 String fileName = clientData.readUTF(); 上失败,但是对于大文件 (31mb),它工作得很好。

在我看来它没有足够的时间。谁能澄清一下?蒂亚

服务器

public void receiveFile() 
{
System.out.println("in receive file");
try {
int bytesRead;

DataInputStream clientData = new DataInputStream(clientSocket.getInputStream());

String fileName = clientData.readUTF();
OutputStream output = new FileOutputStream(("received_from_client_" + fileName));
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}

output.close();
clientData.close();

System.out.println("File " + fileName + " received from Client " + clientID);
} catch (IOException ex) {
System.err.println("ERROR Connection closed Client " + clientID);
}
}

追踪

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at example3.CLIENTConnection.receiveFile(CLIENTConnection.java:75)
at example3.CLIENTConnection.run(CLIENTConnection.java:50)
at java.lang.Thread.run(Thread.java:722)

最佳答案

对方已经关闭了连接。没有更多数据。结束你的结局,忘记它。

关于java - 为什么 .readUTF() over socket 会因小文件而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649080/

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