Search code examples
javaftp-clientapache-commons-net

upload progress in FTPClient


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

Thanks


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