java网络中断管道和接收到的文件损坏

发布于 2024-12-08 19:47:56 字数 252 浏览 3 评论 0原文

我正在尝试编写一个接受文件并使用 DataInputStream 和 BufferedInputStream 将其写入特定目录的服务器。

服务器获取“用户名(字符串)”“文件数量(整数)”“文件名(字符串)”“每个文件的大小(长)”和“未解释字节的文件内容(字节[])”

和如果一切成功,我应该发送布尔值。

但问题是它无法正确接收文件。

有时我会收到“管道损坏”错误消息,或者收到后文件已损坏。

解决了问题..

I am trying to write a server that accepts files and write it in certain directory using DataInputStream and BufferedInputStream.

The server gets 'user name(string)' 'number of files(int)' 'file name(string)' 'size of each file(long)' and 'contents of file which is uninterpreted bytes(byte[])'

and if everything is successful then, I am supposed to send boolean value.

But the problem is that it is not receiving file correctly.

From time to time I get 'broken pipe' error message or the file is corrupted after I receive.

Fixed the problem..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-12-15 19:47:56

一件可能与您的问题有关的小事。您应该将文件大小变量减少实际读取的字节数,而不是请求读取的字节数:

       while(fileSize>0){
            if(fileSize < byteSize)
                byteSize = (int)fileSize;
            int byteRead = din.read(b, 0, byteSize);
            fos.write(b);
            fileSize -= byteRead; // <-- See here
        }

One small thing which may be related to your problem. You should be decrementing your file size variable by the number of bytes actually read, instead of the number of bytes requested to be read:

       while(fileSize>0){
            if(fileSize < byteSize)
                byteSize = (int)fileSize;
            int byteRead = din.read(b, 0, byteSize);
            fos.write(b);
            fileSize -= byteRead; // <-- See here
        }
国粹 2024-12-15 19:47:56

如果在读取输入时,发送者关闭连接,您可能会收到此错误。它可能与您的代码无关。发送方可能超时、在传输完成之前关闭连接或许多其他情况。

看看这个相关问题:如何修复 java.net.SocketException:管道损坏?

You might be getting this error if when reading the input, the sender closes the connection. It probably has nothing to do with your code. The sender might have timed out, closed the connection before the transfer has finished, or many other things.

Take a look at this related question: How to fix java.net.SocketException: Broken pipe?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文