FTPサーバーからファイルをダウンロードする単純なFTPClientクラスがあります。ダウンロードの進行状況も監視する必要がありますが、方法がわかりません。実際にファイルをダウンロードする機能は、
(your ftp client name).retrieveFile(arg1,arg2);
ダウンロードの進行状況を監視するにはどうすればよいですか?
ありがとう、アノン。
FTPサーバーからファイルをダウンロードする単純なFTPClientクラスがあります。ダウンロードの進行状況も監視する必要がありますが、方法がわかりません。実際にファイルをダウンロードする機能は、
(your ftp client name).retrieveFile(arg1,arg2);
ダウンロードの進行状況を監視するにはどうすればよいですか?
ありがとう、アノン。
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 プログラムの場合)。ただし、それはすべてアプリケーション固有の詳細です。