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

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

问题描述

我有一个简单的 FTPClient 类,用于从 FTP 服务器下载文件.我还需要监控下载进度,但我看不出有什么方法.实际下载文件功能是

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

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

如何监控下载进度?

谢谢,匿名.

推荐答案

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

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..

你会做这样的事情:

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 程序)来监控进度,但这都是特定于应用程序的细节.

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天全站免登陆