title: How to get the progress status of the file uploaded to Amazon S3 using Java
body: <p>I'm uploading multiple files to Amazon S3 using Java.</p>
  
  <p>The code I'm using is as follows:</p>
  
  <pre><code>MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  MultiValueMap &lt; String,
  MultipartFile &gt; map = multipartRequest.getMultiFileMap();
  try {
      if (map != null) {
          for (String filename: map.keySet()) {
              List &lt; MultipartFile &gt; fileList = map.get(filename);
              incrPercentge = 100 / fileList.size();
              request.getSession().setAttribute("incrPercentge", incrPercentge);
              for (MultipartFile mpf: fileList) {
  
                  /*
           * custom input stream wrap to original input stream to get
           * the progress
           */
                  ProgressInputStream inputStream = new ProgressInputStream("test", mpf.getInputStream(), mpf.getBytes().length);
                  ObjectMetadata metadata = new ObjectMetadata();
                  metadata.setContentType(mpf.getContentType());
                  String key = Util.getLoginUserName() + "/" + mpf.getOriginalFilename();
                  PutObjectRequest putObjectRequest = new PutObjectRequest(
                  Constants.S3_BUCKET_NAME, key, inputStream, metadata).withStorageClass(StorageClass.ReducedRedundancy);
                  PutObjectResult response = s3Client.putObject(putObjectRequest);
  
              }
          }
      }
  } catch(Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>I have to create the custom input stream to get the number byte consumed by Amazon S3. I got that idea from the question here: <a href="https://stackoverflow.com/questions/3739626/upload-file-or-inputstream-to-s3-with-a-progress-callback">Upload file or InputStream to S3 with a progress callback</a></p>
  
  <p>My <code>ProgressInputStream</code> class code is as follows: </p>
  
  <pre><code>package com.spectralnetworks.net.util;
  import java.io.IOException;
  import java.io.InputStream;
  
  import org.apache.commons.vfs.FileContent;
  import org.apache.commons.vfs.FileSystemException;
  
  public class ProgressInputStream extends InputStream {
      private final long size;
      private long progress,
      lastUpdate = 0;
      private final InputStream inputStream;
      private final String name;
      private boolean closed = false;
  
      public ProgressInputStream(String name, InputStream inputStream, long size) {
          this.size = size;
          this.inputStream = inputStream;
          this.name = name;
      }
  
      public ProgressInputStream(String name, FileContent content)
      throws FileSystemException {
          this.size = content.getSize();
          this.name = name;
          this.inputStream = content.getInputStream();
      }
  
      @Override
      public void close() throws IOException {
          super.close();
          if (closed) throw new IOException("already closed");
          closed = true;
      }
  
      @Override
      public int read() throws IOException {
          int count = inputStream.read();
          if (count &gt; 0) progress += count;
          lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
          return count;
      }@Override
      public int read(byte[] b, int off, int len) throws IOException {
          int count = inputStream.read(b, off, len);
          if (count &gt; 0) progress += count;
          lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
          return count;
      }
  
      /**
       * This is on reserach to show a progress bar
       * @param name
       * @param progress
       * @param lastUpdate
       * @param size
       * @return
       */
      static long maybeUpdateDisplay(String name, long progress, long lastUpdate, long size) {
          /* if (Config.isInUnitTests()) return lastUpdate;
          if (size &lt; B_IN_MB/10) return lastUpdate;
          if (progress - lastUpdate &gt; 1024 * 10) {
              lastUpdate = progress;
              int hashes = (int) (((double)progress / (double)size) * 40);
              if (hashes &gt; 40) hashes = 40;
              String bar = StringUtils.repeat("#",
                      hashes);
              bar = StringUtils.rightPad(bar, 40);
              System.out.format("%s [%s] %.2fMB/%.2fMB\r",
                      name, bar, progress / B_IN_MB, size / B_IN_MB);
              System.out.flush();
          }*/
          System.out.println("name " + name + "  progress " + progress + " lastUpdate " + lastUpdate + " " + "sie " + size);
          return lastUpdate;
      }
  }
  </code></pre>
  
  <p>But this is not working properly. It is printing immediately up to the file size as follows:</p>
  
  <pre><code>name test  progress 4096 lastUpdate 0 sie 30489
  name test  progress 8192 lastUpdate 0 sie 30489
  name test  progress 12288 lastUpdate 0 sie 30489
  name test  progress 16384 lastUpdate 0 sie 30489
  name test  progress 20480 lastUpdate 0 sie 30489
  name test  progress 24576 lastUpdate 0 sie 30489
  name test  progress 28672 lastUpdate 0 sie 30489
  name test  progress 30489 lastUpdate 0 sie 30489
  name test  progress 30489 lastUpdate 0 sie 30489
  </code></pre>
  
  <p>And the actual uploading is taking more time (more than 10 times after printing the lines).</p>
  
  <p>What I should do so that I can get a true upload status?</p>