Monday 2 February 2015

progress bar for file upload with Apache HttpClient 4 and StringEntity

There is a similar solution on stackoverflow, and it uses MultipartEntity to upload a file. However, if you are required to use StringEntity and put file data into JSON POST request like what I needed to do, please see my source code below.



code extract to use HttpPost with StringEntity with JSON in it.
        String jsonRequest = "the file data and related stuff";  
        StringEntity stringEntityForProgressBar = new StringEntity(jsonRequest, progressBean);  
        httpPost.setEntity(stringEntityForProgressBar);  
        HttpResponse response = httpClient.execute(httpPost);  



My own StringEntity.java

 import java.io.IOException;  
 import java.io.OutputStream;  
 import java.io.UnsupportedEncodingException;  
 import com.edentiti.registrations.verification.ProgressBean;  

 public class StringEntity extends org.apache.http.entity.StringEntity  
 {  
      private ProgressBean _progressBean;  
      public StringEntity(String string, ProgressBean progressBean) throws UnsupportedEncodingException  
      {  
           super(string);  
           _progressBean = progressBean;  
      }  
      private OutputStreamProgress outstream;  
      @Override  
      public void writeTo(OutputStream outstream) throws IOException  
      {  
           this.outstream = new OutputStreamProgress(outstream, _progressBean, getContentLength());  
           super.writeTo(this.outstream);  
      }  
 }  



My own StringEntity.java, for my own purpose, the progress goes up to 70% here and you can adjust it(e.g. up to 100%) to suit your own purpose.

 import java.io.IOException;  
 import java.io.OutputStream;  
 import com.edentiti.registrations.verification.ProgressBean;  

 public class OutputStreamProgress extends OutputStream {  
      private final static int CHUNK_SIZE = 40960; // Ming feels it is a lucky number, no solid reason for it.  
      private ProgressBean _progressBean;  
      private long _contentLength;  
   private final OutputStream outstream;  
   private volatile long bytesWritten=0;  
      public OutputStreamProgress(OutputStream outstream, ProgressBean progressBean, long contentLength)  
      {  
           this._progressBean = progressBean;  
           this._contentLength = contentLength;  
     this.outstream = outstream;  
   }  
   @Override  
   public void write(int b) throws IOException {  
     outstream.write(b);  
     bytesWritten++;  
   }  
   @Override  
   public void write(byte[] b) throws IOException {  
           int numChunks;  
           if (b.length % CHUNK_SIZE == 0)  
           {  
                numChunks = b.length / CHUNK_SIZE;  
           }  
           else  
           {  
                numChunks = (b.length / CHUNK_SIZE) + 1;  
           }  
           for (int i = 0; i <= numChunks; i++)  
           {  
                // if last block  
                if (i == numChunks)  
                {  
                     write(b, i * CHUNK_SIZE, b.length);  
                }  
                else  
                {  
                     write(b, i * CHUNK_SIZE, CHUNK_SIZE);  
                }  
           }  
   }  
   @Override  
   public void write(byte[] b, int off, int len) throws IOException {  
     outstream.write(b, off, len);  
     bytesWritten += len;  
           long progressValue = (long) ((70 * getWrittenLength()) / _contentLength);  
           if (progressValue > 70)  
           {  
                progressValue = 70;  
           }  
           if (progressValue < 1)  
           {  
                progressValue = 1;  
           }  
           // here we only allow something between 1 to 70  
           _progressBean.setValue(Long.valueOf(progressValue));  
   }  
   @Override  
   public void flush() throws IOException {  
     outstream.flush();  
   }  
   @Override  
   public void close() throws IOException {  
     outstream.close();  
   }  
   public long getWrittenLength() {  
     return bytesWritten;  
   }  
 }  


References:

  1. http://stackoverflow.com/questions/7057342/how-to-get-a-progress-bar-for-a-file-upload-with-apache-httpclient-4

No comments :

Post a Comment