title: Amazon S3 multiple file uploads using Parts
body: <p>After many hours spent on reading documentations and searching for some tips on the internet, I finally decided to create a Stack OverFlow account; seem desperate? Haha, let's go.</p>
  
  <p>EDIT (05/16/2012) : As I found the solution (by myself, finally), I replaced my old code with my running code.</p>
  
  <p>I go to /admin/lstimg (ListImage), which gets an image (URL) records collection, from my database. Then the collection is set as an attribute which allows me to use the data in my jsp file.</p>
  
  <p>Note : The public class ListImageServlet extends <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html" rel="nofollow noreferrer">HttpServlet</a> and is annotated with <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/MultipartConfig.html" rel="nofollow noreferrer">MultipartConfig</a>.    </p>
  
  <pre><code>    @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException, IOException
      {
          try
          {
              Collection images = ImageFacade.getIjc().findImageEntities();
              request.setAttribute("images", images);
              request.getRequestDispatcher("/adm/img/lstimg.jsp").forward(request, response);
          }
  
          catch (Exception ex)
          {
              Logger.getLogger(ListImageServlet.class.getName()).log(Level.SEVERE, "Cannot retrieve the images.", ex);
          }
      }
  </code></pre>
  
  <p>In my jsp, there's a large form (as my "images" collection is). Here, I use the data from the "images" collection. For each image, in the first column, a thumbnail and in the second column, a file input. I'm trying to perform multiple updates through this (single) form.</p>
  
  <pre><code>&lt;form method="post" action="../admin/lstimg" enctype="multipart/form-data"&gt;
  &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Thumb&lt;/th&gt;
              &lt;th&gt;to update&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;c:forEach items="${images}" var="image"&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;img src="myURL" height="100"/&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;input type="file" name="${image.thumb}"/&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;input type="submit" value="Proceed" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>My first question is :
  As I need the new image to have the same "image.thumb" (which is the Key), I set the file input name to the current "image.thumb" but I was wondering if, I'll have some issues to retrieve my file on my computer? As my file on my computer has its own path/filename and the one on my server has another path/UUID (Key).</p>
  
  <p>EDIT (05/16/2012) : As the AWS doesn't ask for the file path (to be manually set), no issue with this.</p>
  
  <p>How the form looks (<a href="https://i.stack.imgur.com/LdQNa.png" rel="nofollow noreferrer">Web Browser</a>)</p>
  
  <p>After I choosed the image(s) I need to update/change, I click on the Submit button (Proceed).</p>
  
  <ol>
  <li>I get all the Parts (file inputs) and store them in a collection "allParts".</li>
  <li>I check every part of the "allParts" collection, if the file size is between 1b and 10Mo, the part is added to the "selectedParts" collection.</li>
  <li>Here, I don't know how to do what I want to do... All my images are hosted in my Amazon S3 Server. I use the Jets3t Toolkit. For the first upload, I call the  buildPostForm method (below) and it does everything.</li>
  </ol>
  
  <pre><code>S3Service.buildPostForm(String bucketName, String key, ProviderCredentials credentials, Date expiration, String[] conditions, String[] inputFields, String textInput, boolean isSecureHttp);</code></pre>
  
  <pre><code>
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException
      {
          try
          {
              ImageCheck.initAS3(request);
              ImageCheck.update(request, response);
          } 
  
          catch (S3ServiceException ex) 
          {
              Logger.getLogger(ListImageServlet.class.getName()).log(Level.SEVERE, null, ex);
          } 
  
          catch (ServiceException ex) 
          {
              Logger.getLogger(ListImageServlet.class.getName()).log(Level.SEVERE, null, ex);
          } 
  
          catch (NoSuchAlgorithmException ex) 
          {
              Logger.getLogger(ListImageServlet.class.getName()).log(Level.SEVERE, null, ex);
          } 
  
          catch (IllegalStateException ex) 
          {
              Logger.getLogger(ListImageServlet.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          response.sendRedirect("../admin/lstimg");
      }
  </code></pre>
  
  <p>My second question is :
  To upload/change a file through the servlet, as I use my own form, I need to set the File to my S3Object, but I don't know what I can do with my "selectedParts"? Is there a way to change a Part to a File? Do I have to use something else than Part?</p>
  
  <p>EDIT (05/16/2012) : <a href="https://stackoverflow.com/a/10604206/1391944">My solution</a></p>
  
  <p>Sorry for my english.
  Thanks by advance for your suggestions, advices, answers.
  If you have any question regarding the code or if you need some more, don't hesitate.</p>