Java 使用Apache Commons FTPClient监视进度

Java 使用Apache Commons FTPClient监视进度,java,ftp,download,apache-commons,ftp-client,Java,Ftp,Download,Apache Commons,Ftp Client,我有一个简单的FTPClient类,可以从FTP服务器下载文件。我还需要监控下载进度,但我看不出有什么办法。实际下载文件功能是的一个简单功能 (您的ftp客户端名称).retrieveFile(arg1、arg2) 如何监控下载进度 谢谢, 另外,您需要一个CountingOutputStream(如Commons IO:上所示)。您可以创建其中一个,将目标OutputStream封装在其中,然后根据需要检查字节数以监控下载进度 编辑:您可以这样做: int size; String remot

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

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

如何监控下载进度

谢谢,
另外,

您需要一个CountingOutputStream(如Commons IO:上所示)。您可以创建其中一个,将目标OutputStream封装在其中,然后根据需要检查字节数以监控下载进度

编辑:您可以这样做:

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程序)来监视进度,但这是所有特定于应用程序的详细信息。

您需要与它们的“哈希”函数等效的函数-不知道它是什么。我读过一些关于copyStreamAdapter的文章,但不知道它的任何详细信息。