有時候寫程序跟小時候做數學題一樣粗心。
今天寫了個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次。。。。。
記錄!!!