我看了很多例子,並試圖理解我做錯了什麼,但沒有成功,也許你可以幫助我。它總是停在第二個文件上,但第一個文件只是在c:\上大小爲0kb。 files_server.get(i)是ArrayList,其中包含我希望下載的所有文件。Java FTP下載進度
我的代碼:
public FTPConnection() {
StartD std = new StartD();
std.start();
}
class StartD extends Thread{
@Override
public void run()
{
for (int i = 0; i < files_server.size(); i++) {
err = ftpDownload(files_server.get(i), "C:/"+ files_server.get(i));
if (!err)
{
System.out.println("Error in download, breaking");
break;
}
}
}
public boolean ftpDownload(String srcFilePath, String desFilePath)
{
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);
InputStream input = mFTPClient.retrieveFileStream(srcFilePath);
byte[] data = new byte[1024];
int count;
while ((count = input.read(data)) != -1)
{
desFileStream.write(data, 0, count);
}
desFileStream.close();
} catch (Exception e) {
return false;
}
return true;
}}
如果我使用finction:
public boolean ftpDownload(String srcFilePath, String desFilePath) {
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);
status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
}
return status;
}
代替,一切都運行得很好,但我不能老是監視文件下載進度。
很多問題。例如,由於您沒有說出所有這些FTP的來源,代碼不能被複制。前1024個字節的數據永遠丟失。你應該附加一個可運行的測試用例 – Raffaele 2012-08-15 08:11:54
你是什麼意思「代碼不能被複制」? 已修復「前1024個字節的數據永遠丟失」。還是行不通。 – DanM 2012-08-15 09:05:40
[this](http://pastebin.com/ng5rw4sV)是可複製的,自包含代碼 – Raffaele 2012-08-15 10:28:16