【问题标题】:upload progress in FTPClient在 FTPClient 中上传进度
【发布时间】:2012-03-12 20:02:14
【问题描述】:

我正在使用 commons-net FTPClient 上传一些文件。
如何获得上传进度(现在上传的字节数)?

谢谢

【问题讨论】:

标签: java ftp-client apache-commons-net


【解决方案1】:

当然,只需使用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) { ... }

【讨论】:

  • 我使用了这个解决方案,方法 public void bytesTransferred(long totalBytesTransferred,int bytesTransferred, long streamSize) 在每个字节传输后调用,我无法更新我的 UI,因为它的线程被 UI 更新调用淹没了.我在android中使用过任何人都可以帮助为什么会发生@Lukasz
【解决方案2】:

我认为 CountingOutputStream 对我们来说可能更好,因为它似乎就是为了这个目的?

这里有人回答:Monitoring progress using Apache Commons FTPClient

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 2011-08-30
    • 2011-08-18
    • 2015-07-28
    • 2013-07-07
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多