我看了很多例子,并试图理解我做错了什么,但没有成功,也许你可以帮助我。它总是停在第二个文件上,但第一个文件只是在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