9

FTPサーバーからファイルをダウンロードする単純なFTPClientクラスがあります。ダウンロードの進行状況も監視する必要がありますが、方法がわかりません。実際にファイルをダウンロードする機能は、

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

ダウンロードの進行状況を監視するにはどうすればよいですか?

ありがとう、アノン。

4

1 に答える 1

19

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 プログラムの場合)。ただし、それはすべてアプリケーション固有の詳細です。

于 2011-05-03T21:53:37.307 に答える