在FTPClient中上传进度

RYN*_*RYN 4 java ftp-client apache-commons-net

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

谢谢

Łuk*_*nek 5

当然,只需使用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) { ... }
Run Code Online (Sandbox Code Playgroud)