我得到了这个客户端应用程序,它将我的文件完全发送到服务器。但我希望它以 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/