简体   繁体   English

用Java块发送文件

[英]Sending file in chunks in Java

I got this client application that sends my file fully to server. 我得到了这个客户端应用程序,可以将我的文件完全发送到服务器。 But I want it to send file in chunks. 但是我希望它以大块发送文件。 Here is my client code: 这是我的客户代码:

byte[] fileLength = new byte[(int) file.length()];  

        FileInputStream fis = new FileInputStream(file);  
        BufferedInputStream bis = new BufferedInputStream(fis);

        DataInputStream dis = new DataInputStream(bis);     
        dis.readFully(fileLength, 0, fileLength.length);  

        OutputStream os = socket.getOutputStream();  

        //Sending size of file.
        DataOutputStream dos = new DataOutputStream(os);   
        dos.writeLong(fileLength.length);
        dos.write(fileLength, 0, fileLength.length);     
        dos.flush();  

        socket.close();  

So how can I make client send my file in chunks? 那么,如何使客户端分块发送文件? Thanks in advance. 提前致谢。

Try to send the file from client in parts, something like 尝试部分地从客户端发送文件,例如

int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}

and reassemble it on the server. 并在服务器上重新组装。

Apache Commons supports streaming, so it may help. Apache Commons支持流传输,因此可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM