【发布时间】:2013-06-26 07:04:33
【问题描述】:
我正在尝试使用 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();
writer 完成发送字节总数,而 reader 没有获取所有字节并循环等待它
【问题讨论】:
-
能否指出我代码中的错误。
-
...您可以尝试在不使用
BufferedInputStream的情况下直接使用cli.InputStream() -
幽默一下 - 你是否知道一个事实,例如通过检查数据,文件的尾部还没有到达?只是为了排除 K=1024 与 K=1000 之间的差异,或实际大小与所需磁盘块等的差异。
-
@Selvin 他当然可以,但为什么呢?不会有任何区别。
-
关闭你的写入怎么样?我看到你打电话给
bout.flush(),但从来没有打电话给bout.close()。