0
我使用插座在的java文件傳輸。 這裏是客戶代碼Java文件收到停止
for(int i = 0;i < fileList.size();i++) {
String filename = (String)fileList.get(i);
RequestFile(filename);
try {
BufferedOutputStream fileWriter = new BufferedOutputStream(
new FileOutputStream(
new File(PROGRAM_PATH + "/" +
filename)));
int packet;
int count = 0;
while((packet = fileReader.read()) != -1) {
fileWriter.write(packet);
count++;
}
System.out.println(filename + " receiver complete. (count : " + count + ")");
fileWriter.flush();
fileWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
這裏是服務器代碼
public void SendFile(String filename) {
try {
fileReader = new BufferedInputStream(new FileInputStream(CLIENT_PATH + "/" + filename));
int packet;
int count = 0;
while((packet = fileReader.read()) != -1) {
count++;
fileWriter.write(packet);
}
fileWriter.write(-1);
System.out.println(count);
fileReader.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
當我檢查了服務器的數量,這是635
。這意味着服務器已發送數據635
次。 但是,客戶的打印計數只有512
。 (從0到511),我認爲這是停在read()
,因爲
System.out.println(filename + " receiver complete. (count : " + count + ")");
不打印。有人能告訴我理由和解決方案嗎?
我想你應該使用'packet'來增加'count'並檢查是否所有的字節都被傳輸了。 – Tirath 2014-10-27 12:10:11