使用Apache Commons FTPClient监控进度
我有一个简单的FTPClient类,从FTP服务器下载文件。 我还需要监控下载的进度,但我没有看到怎样的方式。 实际下载文件function是一个简单的function
(your ftp client name).retrieveFile(arg1,arg2);
如何监控下载进度?
谢谢,Anon。
您需要一个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();
如果您的程序是multithreading的,您可能希望使用单独的线程监视进度(例如,对于GUI程序),但这是所有特定于应用程序的详细信息。
- 在GenericObjectPool中创建对象
- 根据Java中的元素属性将列表拆分为多个子列表
- 使用Linux上的Apache Commons Compression压缩文件时编码错误
- 如何将字符串参数传递给使用Apache Commons Exec启动的可执行文件?
- 是否有StringUtils.isNumeric的替代方法,我的意思是什么?
- Spring Framework是否支持多部分请求中的流模式
- Apache DefaultHttpClient调用导致“java.lang.RuntimeException:Stub!”
- FTPClient.listFiles无法正常工作
- 用Java确定另一个国家的互联网时间