Question

I'm using commons-net FTPClient to upload some files.
How can I get progress of upload (number of bytes uploaded up now)?

Thanks

Was it helpful?

Solution

Sure, just use CopyStreamListener. Below you will find an example (copied from commons-io wiki) of file retrieval, so You can easily change it other-way-round.

    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) { ... }

OTHER TIPS

I think perhaps it is better to us the CountingOutputStream since it seems intended for this very purpose ?

This is answered by someone here: Monitoring progress using Apache Commons FTPClient

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top