我从客户端接收数据时收到UTFDataFormatException

时间:2018-11-29 19:31:16

标签: java client client-server inputstream utf

我正在发送文件扩展名,并且接收到从客户端到服务器文件扩展名的数据,但是当我开始接收文件时,出现此异常 java.io.UTFDataFormatException:字节0周围的输入格式错误 这是我收到的服务器端代码

DataInputStream in= new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    input= in.readUTF();// *I get error here when i start receiving file*
    }
    if(input.startsWith("reply")){
    addTask(input.substring(5));
    }

这是我的服务器类代码: This code sends extension which works fine and extension is well received at server side

    void sendReply(String extension,Socket s) throws IOException {
    DataOutputStream out = new DataOutputStream(s.getOutputStream());
    out.writeUTF("ext"+extension);
}

此代码发送文件:

DataOutputStream dos;
        dos= new DataOutputStream(socket.getOutputStream());
            try (FileInputStream fis = new FileInputStream(f)) {
                byte[] buffer = new byte[4096];
                while(fis.read(buffer)>0)
                {
                    dos.write(buffer);
                }   
            }
        dos.close();

0 个答案:

没有答案
相关问题