假设这些:
ObjectOutputStream out =
new ObjectOutputStream(new BufferedOutputStream(remote.getOutputStream()));
ObjectInputStream in =
new ObjectInputStream(new BufferedInputStream(remote.getInputStream()));
发送“启动文件发送”消息和大小并成功读取另一端的字符串和长号后(您可以假设我已经处理过它们)我希望能够编写整个文件而无需实例新的OutputStream。 到目前为止我所做的是:
InputStream buf = new BufferedInputStream(new FileInputStream(file));
out.writeUnshared(new PeerProtocol(PeerProtocol.Type.FILE, file.getName(), file.length()));
int sum = 0, len;
byte[] buffer = new byte[remote.getSendBufferSize()];
out.flush();
out.reset();
while ((len = buf.read(buffer)) > 0) {
out.write(buffer, 0, len);
sum += len;
sendFileDialogController.updatePercentage((double) sum / file.length() * 100);
}
// System.out.println("done sending");
//out.flush(); didnt work neither
sendFileDialogController.updatePercentage(100);
buf.close();
问题是我总是丢失最后几个字节,因此文件已损坏且无法使用。如何解决这个问题?