Java“无效的流标题”错误

时间:2014-05-13 20:25:28

标签: java file sockets

我在程序中将文件从一个人发送到另一个人时遇到问题,我得到一个"无效的流标题"错误。以下是分别发送和接收文件的代码:

private void sendFile(Socket socket, File file) throws IOException
{
    int bytesRead;
    byte[] buffer;
    FileInputStream input;
    OutputStream output;
    buffer = new byte[(int)file.length()];
    input = new FileInputStream(file);
    output = socket.getOutputStream();
    bytesRead = input.read(buffer);

    while(bytesRead > 0)
    {
        output.write(buffer,0,bytesRead);
        bytesRead = input.read(buffer);
    }

    input.close();
    output.close();
}



private void receiveFile(Socket socket, File file) throws IOException
{
    int bytesRead;
    byte[] buffer;
    InputStream input;
    FileOutputStream output;
    buffer = new byte[(int)file.length()];
    output = new FileOutputStream(file);
    input = socket.getInputStream();
    bytesRead = input.read(buffer);

    while(bytesRead > 0)
    {
        output.write(buffer,0,bytesRead);
        bytesRead = input.read(buffer);
    }

    input.close();
    output.close();
}

我已经阅读了Stack,我尝试过的所有东西都没有修复它。有没有人有任何想法?谢谢!

确切错误是:

java.io.StreamCorruptedException: invalid stream header: 3C736372
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at Message.readMessageFrom(Message.java:32)
    at Server$Handler.run(Server.java:220)
    at java.lang.Thread.run(Thread.java:744)

0 个答案:

没有答案