2

我正在通过套接字将文件从客户端传输到基本服务器。当我传输小文件(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)
4

1 回答 1

0

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

于 2013-02-01T19:52:37.833 回答