0
你好,我需要一個FTP下載狀態欄。 我想得到一個浮動< = 1的進展。這是我的代碼:Java的ftp下載狀態欄
float status=0;
FTPFile[] files = ftp.listFiles(REMOTEFILE);
if (files == null || files.length == 0) {
throw new FileNotFoundException();
}
long size = files[0].getSize();
InputStream inputStream = ftp.retrieveFileStream(REMOTEFILE);
byte buf[] = new byte[1024];
int len;
int download=0;
while ((len = inputStream.read(buf)) > 0){
out.write(buf, 0, len);
download+=1024;
status=(float)download/size; // here it set the progress
}
out.close();
我的問題是,在最後的狀態是超過1,我認爲是在1024緩衝區大小(以不完全緩衝的情況下,在最後)。也許是因爲下載的字節數多於文件[0] .getSize()給我的數量?
謝謝。
但read()不讀取1024的塊? – Tobia
從javadoc; '從輸入流中讀取一些字節數並將它們存儲到緩衝區數組b中。實際讀取的字節數作爲整數返回 – Qwerky