我正在尝试从文件大小为100 Kb的服务器读取数据。 所以我打破了数据并发送了6个文件。我可以在服务器上看到文件被正确分解为5个100 kb的块,其余的是74 kb左右。 在客户端我试图接收这6个文件,所以我保持缓冲区大小为100但我得到7个文件。 这是我的代码的一部分
try {
sock = new Socket(server, port);
System.out.println("Connecting...");
int buffer = 100 * 1000, bytesRead = 0, counter = 0;
// receive file
BufferedInputStream bis = new BufferedInputStream(sock.getInputStream(), buffer);
byte[] mybytearray = new byte[buffer];
while ((bytesRead = bis.read(mybytearray))!=-1) {
String fileName = "C:/Users/Desktop/Client2/" + counter;
File newFile = new File(fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
bos.write(mybytearray, 0, bytesRead);
bos.flush();
bos.close();
System.out.println("File " + counter + " downloaded (" + bytesRead + " bytes read)");
counter++;
}
}
我得到的结果就是这个。
File 0 downloaded (35040 bytes read)
File 1 downloaded (100000 bytes read)
File 2 downloaded (100000 bytes read)
File 3 downloaded (100000 bytes read)
File 4 downloaded (100000 bytes read)
File 5 downloaded (100000 bytes read)
File 6 downloaded (39346 bytes read)
我做错了什么?