我正在尝试使用 Java 中的套接字发送文件。问题是这样的:假设有一个 97kb 的文件。它得到大约 95.8kb 并等待更多,但作者已发送所有 97kb。
读法:
FileOutputStream fout = new FileOutputStream(fl);
int counter = 0;
byte[] byt = new byte[8192];
BufferedInputStream bin = new BufferedInputStream(cli.InputStream());
int count = 0;
while((count = bin.read(byt)) > 0)
{
counter = counter + count;
Log.d("TINTERACT", String.valueOf(count) + " _" + String.valueOf(counter) + " _" + String.valueOf(size));
fout.write(byt, 0, count);
}
fout.flush();
fout.close();
而写作是:
System.out.println("Starting writing");
FileInputStream fIn = new FileInputStream(path);
byte[] byt = new byte[8192];
BufferedInputStream bin = new BufferedInputStream(fIn);
BufferedOutputStream bout = new BufferedOutputStream(ser.OutputStream());
int count = 0, countr = 0;
while((count = bin.read(byt)) > 0)
{
System.out.println(count);
bout.write(byt, 0, count);
countr = countr + count;
}
bout.flush();
System.out.println("sent " + countr + "End");
bin.close();
写入器完成发送总字节数,而读取器未获取所有字节并循环等待它