使用Apache Commons FTPClient监控进度 [英] Monitoring progress using Apache Commons FTPClient

查看:677
本文介绍了使用Apache Commons FTPClient监控进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的FTPClient类,可以从FTP服务器下载文件。我也需要监控下载的进度,但是我看不到方法。实际的下载文件功能是一个简单的功能,它是一个简单的功能。

(您的ftp客户端名称).retrieveFile(arg1,arg2);



如何监控下载进度?



谢谢,
Anon。 p>

解决方案

您需要一个CountingOutputStream(如Commons IO所示: http://commons.apache.org/io/api-release/index.html )。您创建其中之一,将目标OutputStream包装在其中,然后您可以根据需要检查ByteCount以监控下载进度。



编辑:你会如下所示:

  int size; 
String remote,local;

//做一些工作来初始化大小,远程和本地文件路径
//将remoteSource保存到本地
OutputStream output = new FileOutputStream(local);
CountingOutputStream cos = new CountingOutputStream(output){
protected void beforeWrite(int n){
super.beforeWrite(n);

System.err.println(已下载+ getCount()+/+ size);
}
};
ftp.retrieveFile(remote,cos);

output.close();

如果您的程序是多线程的,您可能需要使用单独的线程监视进度(例如,一个GUI程序),但这是所有应用程序特定的细节。


I have a simple FTPClient class that downloads files form an FTP server. I also need to monitor progress of the download, but I do not see a way how. The actually download files function is a simple function of

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

How can I monitor the download progress?

Thanks, Anon.

解决方案

You need a CountingOutputStream (as seen on Commons IO: http://commons.apache.org/io/api-release/index.html). You create one of those, wrap your destination OutputStream in it, and then you can check the ByteCount on demand to monitor the download progress..

EDIT: You'd do something like this:

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();

If your program is multithreaded you might want to monitor the progress using a separate thread (for example, for a GUI program), but that's all application-specific detail.

这篇关于使用Apache Commons FTPClient监控进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆