2011-05-03 107 views
8

我有一个简单的FTPClient类,用于从FTP服务器下载文件。我也需要监视下载的进度,但我没有看到如何。实际下载文件功能是使用Apache Commons监视进度FTPClient

(your ftp client name).retrieveFile(arg1,arg2);

简单的功能如何监控下载进度?

谢谢, 匿名。

+0

你需要相当于他们的“散列”功能 - 不知道它是什么。 – duffymo 2011-05-03 21:21:40

+0

我没有读过关于copyStreamAdapter的一些信息,但我不知道有关它的任何细节。 – 2011-05-03 21:49:17

回答

18

您需要一个CountingOutputStream(如Commons IO上所示:http://commons.apache.org/io/api-release/index.html)。创建其中的一个,包你的目的地的OutputStream中,然后就可以检查需要监视下载进度BYTECOUNT ..

编辑:你会做这样的事情:

int size; 
String remote, local; 

// do some work to initialize size, remote and local file path 
// before saving remoteSource to local 
OutputStream output = new FileOutputStream(local); 
CountingOutputStream cos = new CountingOutputStream(output){ 
    protected void beforeWrite(int n){ 
     super.beforeWrite(n); 

     System.err.println("Downloaded "+getCount() + "/" + size); 
    } 
}; 
ftp.retrieveFile(remote, cos); 

output.close(); 

如果你的程序是多线程的,你可能想用一个单独的线程来监视进程(例如,用于一个GUI程序),但这些都是特定于应用程序的细节。

+1

我可以看一个例子吗? – 2011-05-03 22:04:23