我有一个简单的 FTPClient 类,可以从 FTP 服务器下载文件。我还需要监视下载的进度,但我看不出如何。实际下载文件功能是一个简单的功能
(your ftp client name).retrieveFile(arg1,arg2);
如何监控下载进度?
谢谢,阿农。
我有一个简单的 FTPClient 类,可以从 FTP 服务器下载文件。我还需要监视下载的进度,但我看不出如何。实际下载文件功能是一个简单的功能
(your ftp client name).retrieveFile(arg1,arg2);
如何监控下载进度?
谢谢,阿农。
您需要一个 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 程序),但这都是特定于应用程序的细节。