有时候写程序跟小时候做数学题一样粗心。
今天写了个demo,Socket收发文件的,结果客户端只收到50%的文件内容。
纠结了半天,才发现错误代码:
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
LogTrace.d(TAG, "doInBackground", "read = "+fis.read(buf));
}
passedlen += read;
if (read == -1) {
break;
}
System.out.println("文件传送了 passedlen:" + passedlen);
System.out.println("文件传送了" + (passedlen * 100 / len) + "%\n");
ps.write(buf, 0, read);
}
ps.flush();LogTrace.d(TAG, "doInBackground", "read = "+fis.read(buf));
读了2次。。。。。
记录!!!
本文记录了一个Socket文件传输程序中出现的问题:客户端仅接收到一半的数据。经过排查发现原因是代码中文件输入流被读取了两次,导致实际传输的数据量减少。
1396

被折叠的 条评论
为什么被折叠?



