java - 在Java中以 block 的形式发送文件

标签 java

我得到了这个客户端应用程序,它将我的文件完全发送到服务器。但我希望它以 block 的形式发送文件。这是我的客户端代码:

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();  

那么如何让客户端分块发送我的文件呢?提前致谢。

最佳答案

尝试从客户端分部分发送文件,例如

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

并在服务器上重新组装它。

Apache Commons支持流式传输,因此可能会有所帮助。

关于java - 在Java中以 block 的形式发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450727/

相关文章:

java - 使用 Rest API 时如何创建列表参数

java - 在不同的类中使用执行器并同步 CountDownLatch

java - 如何在 SharedPreferences 中存储和检索 Location 对象?

java - Eclipse插件自动更新安装

java - Android 相机可以在模拟器上使用,但不能在手机上使用?

Java 数组从另一个类访问它并用作组合框

java - 使用 HttpClient/MultipartEntity 流式传输上传

java - 找不到库 "libmaliinstr.so".... 在 Eclipse 中

java - 日期时间不显示

java - 无法在基于 Java 的 Web 服务或 Eclipse 中包含 Java 库 (.jar)