有时候写程序跟小时候做数学题一样粗心。
今天写了个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次。。。。。
记录!!!