为什么 java 套接字中的客户端文件会损坏?

发布于 2024-11-11 10:00:28 字数 740 浏览 0 评论 0原文

我编写了一个java代码,它使用FileInputStreamBufferedInputStream.exe文件从服务器发送到客户端,但是该文件在以下位置被损坏客户端。

可能是什么原因?

command1= ServerFrame.msg1+".exe";
File p=new File(command1);
FileInputStream f=new FileInputStream(p);
BufferedInputStream bis=new BufferedInputStream(f);
Integer d=bis.available();
int d1=d;
byte b[]=new byte[d];
bis.read(b,0,d1);
System.out.println(d1);
dos=new DataOutputStream(s.getOutputStream());
BufferedOutputStream bos=new BufferedOutputStream(s.getOutputStream());
dos.writeUTF(d.toString());             // sending length in long
bos.write(b,0,d1);                      // sending the bytess

bos.flush();
bis.close();  
bos.close();
dos.close();      

I have written a java code which sends a .exe file from the server to the client using FileInputStream and BufferedInputStream, but the file gets corrupted at the client side.

What could be the reason?

command1= ServerFrame.msg1+".exe";
File p=new File(command1);
FileInputStream f=new FileInputStream(p);
BufferedInputStream bis=new BufferedInputStream(f);
Integer d=bis.available();
int d1=d;
byte b[]=new byte[d];
bis.read(b,0,d1);
System.out.println(d1);
dos=new DataOutputStream(s.getOutputStream());
BufferedOutputStream bos=new BufferedOutputStream(s.getOutputStream());
dos.writeUTF(d.toString());             // sending length in long
bos.write(b,0,d1);                      // sending the bytess

bos.flush();
bis.close();  
bos.close();
dos.close();      

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

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

发布评论

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

评论(3

神魇的王 2024-11-18 10:00:28

我想 s 是你的套接字。您的代码中几乎没有什么可以修改的:

  • bis.available() 返回可以在不阻塞的情况下读取的字节数,而不是文件的总大小,您应该使用循环来读取您在两个不同缓冲区中使用输出流的文件
  • ,并且在不刷新的情况下写入这两个缓冲区;另外,你为什么要写UTF?

这是您打算执行的操作:

private void copy(InputStream in, OutputStream out) {
    byte[] buf = new byte[0x1000];
    int r;
    while ((r = in.read(buf)) >= 0) {
        out.write(b, 0, r);
    }
}
InputStream in = new BufferedInputStream(new FileInputStream(path));
OutputStream out = new BufferedOutputStream(s.getOutputStream());
copy(in, out);
in.close();
out.close();

I suppose that s is your socket. There are few thing that can be wong in your code:

  • bis.available() returns the number of bytes that can be read without bocking, not the total size of the file, you should use a loop to read the file
  • you use the output stream in two different buffers and you write to both of them without flushing; also, why are you writing UTF?

Here is what you intend to do:

private void copy(InputStream in, OutputStream out) {
    byte[] buf = new byte[0x1000];
    int r;
    while ((r = in.read(buf)) >= 0) {
        out.write(b, 0, r);
    }
}
InputStream in = new BufferedInputStream(new FileInputStream(path));
OutputStream out = new BufferedOutputStream(s.getOutputStream());
copy(in, out);
in.close();
out.close();
贱人配狗天长地久 2024-11-18 10:00:28

bis.available() 返回可供读取的字节,它可能不是完整的内容大小,您必须循环读取直到到达 EOF。

bis.available() returns the bytes available for read, it may not be the full content size, u have to read in a loop till it reaches EOF.

窗影残 2024-11-18 10:00:28

如果有人遇到同样的问题,缓冲区大小是这种情况下的罪魁祸首:

Integer d=bis.available(); 
byte b[]=new byte[d];

应该较小,尝试 1024 或其他东西:

byte b[]=new byte[1024];

希望这会有所帮助..

in case someone stuck with same problem, buffer size is the culprit in this case :

Integer d=bis.available(); 
byte b[]=new byte[d];

It should be lesser try 1024 or something:

byte b[]=new byte[1024];

hope this helps..

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