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

时间:2013-02-01 15:01:24

标签: java sockets

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

在我看来它没有足够的时间。有人可以澄清一下吗? TIA

SERVER

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)

1 个答案:

答案 0 :(得分:0)

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