How to get a progress bar for a file upload with Apache HttpClient 4?

前端 未结 4 1800
暗喜
暗喜 2020-11-29 22:46

I\'ve got the following code for a file upload with Apache\'s HTTP-Client (org.apache.http.client):

  public static void main(String[] args) throws Exception         


        
4条回答
  •  再見小時候
    2020-11-29 23:17

    This answer extends kilaka's answer by adding a simple listener to the OutputStreamProgress.java class instead of having the public getProgress() method (I'm honestly not sure how you are suppose to call the getProgress() method since the thread will be executing inside of httpclient's code the entire time you might want to call getProgress()!).

    Please note you'll need to extend the entity class for each entity type you want to use, and when you write your HttpClient code, you'll need to create the entity of that new type.

    I wrote a very basic write listener that implements the WriteListener interface. This is where you'll add your logic to do something with the write reports from the OutputStreamProgress, something like updating a progress bar :)

    Big thanks to kilaka for using the decorator idea to sneak in a counting outstream.

    WriteLisener.java

    public interface WriteListener {
        void registerWrite(long amountOfBytesWritten);
    }
    

    OutputStreamProgress.java

    import java.io.IOException;
    import java.io.OutputStream;
    
    public class OutputStreamProgress extends OutputStream {
    
        private final OutputStream outstream;
        private long bytesWritten=0;
        private final WriteListener writeListener;
        public OutputStreamProgress(OutputStream outstream, WriteListener writeListener) {
            this.outstream = outstream;
            this.writeListener = writeListener;
        }
    
        @Override
        public void write(int b) throws IOException {
            outstream.write(b);
            bytesWritten++;
            writeListener.registerWrite(bytesWritten);
        }
    
        @Override
        public void write(byte[] b) throws IOException {
            outstream.write(b);
            bytesWritten += b.length;
            writeListener.registerWrite(bytesWritten);
        }
    
        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            outstream.write(b, off, len);
            bytesWritten += len;
            writeListener.registerWrite(bytesWritten);
        }
    
        @Override
        public void flush() throws IOException {
            outstream.flush();
        }
    
        @Override
        public void close() throws IOException {
            outstream.close();
        }
    }
    

    BasicWriteListener

    public class BasicWriteListener implements WriteListener {
    
    public BasicWriteListener() {
        // TODO Auto-generated constructor stub
    }
    
    public void registerWrite(long amountOfBytesWritten) {
        System.out.println(amountOfBytesWritten);
    }
    
    }
    

    MultipartEntityWithProgressBar

    import java.io.IOException;
    import java.io.OutputStream;
    import java.nio.charset.Charset;
    
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntity;
    
    public class MultipartEntityWithProgressBar extends MultipartEntity {
        private OutputStreamProgress outstream;
        private WriteListener writeListener;
    
        @Override
        public void writeTo(OutputStream outstream) throws IOException {
            this.outstream = new OutputStreamProgress(outstream, writeListener);
            super.writeTo(this.outstream);
        }
    
        public MultipartEntityWithProgressBar(WriteListener writeListener)
        {
            super();
            this.writeListener = writeListener;
        }
        public MultipartEntityWithProgressBar(HttpMultipartMode mode, WriteListener writeListener)
        {
            super(mode);
            this.writeListener = writeListener;
        }
        public MultipartEntityWithProgressBar(HttpMultipartMode mode, String boundary, Charset charset, WriteListener writeListener)
        {
            super(mode, boundary, charset);
            this.writeListener = writeListener;
        }
    
        // Left in for clarity to show where I took from kilaka's answer
    //  /**
    //   * Progress: 0-100
    //   */
    //  public int getProgress() {
    //      if (outstream == null) {
    //          return 0;
    //      }
    //      long contentLength = getContentLength();
    //      if (contentLength <= 0) { // Prevent division by zero and negative values
    //          return 0;
    //      }
    //      long writtenLength = outstream.getWrittenLength();
    //      return (int) (100*writtenLength/contentLength);
    //  }
    
    }
    

提交回复
热议问题