我正在使用 commons-net FTPClient 上传一些文件。
如何获得上传进度(现在上传的字节数)?
谢谢
当然,只需使用CopyStreamListener。您将在下面找到文件检索的示例(从 commons-io wiki 复制),因此您可以轻松地对其进行更改。
try {
InputStream stO =
new BufferedInputStream(
ftp.retrieveFileStream("foo.bar"),
ftp.getBufferSize());
OutputStream stD =
new FileOutputStream("bar.foo");
org.apache.commons.net.io.Util.copyStream(
stO,
stD,
ftp.getBufferSize(),
/* I'm using the UNKNOWN_STREAM_SIZE constant here, but you can use the size of file too */
org.apache.commons.net.io.CopyStreamEvent.UNKNOWN_STREAM_SIZE,
new org.apache.commons.net.io.CopyStreamAdapter() {
public void bytesTransferred(long totalBytesTransferred,
int bytesTransferred,
long streamSize) {
// Your progress Control code here
}
});
ftp.completePendingCommand();
} catch (Exception e) { ... }
我认为 CountingOutputStream 对我们来说可能更好,因为它似乎就是为了这个目的?
这里有人回答了这个问题:Monitoring progress using Apache Commons FTPClient