title: Glassfish - uploading images - doing it right
body: <p>I am on latest glassfish (3.1.2) - so no need for apache FileItem and no bugs with getPart(). I read that the best practice on uploading images is saving them on the file system (see <a href="https://stackoverflow.com/questions/348363/what-is-the-best-practice-for-storing-uploaded-images">here</a> for instance). I am editing already existing code - smelly at that - so I had the idea to do :</p>
  
  <pre><code>Part p1 = request.getPart("file");
  System.out.println("!!!!!P1 : " + p1);
  </code></pre>
  
  <p>Prints :</p>
  
  <pre><code>!!!!!P1 : File name=DSC03660.JPG, 
  StoreLocation=C:\_\glassfish3\glassfish\domains\domain1\generated\jsp\elkethe\upload_7cb06306_138b413999a__7ffa_00000000.tmp, 
  size=2589152bytes, isFormField=false, FieldName=file
  </code></pre>
  
  <p>newlines mine. In the code people are doing :</p>
  
  <pre><code>if (request.getParameter("crop") != null) {
      // get path on the server
      String outputpath = this.getServletContext().getRealPath(
              "images/temp/" + session.getId() + ".jpg");
      // store photo
      InputStream is = p1.getInputStream();
      createPhoto(is, outputpath);
      session.setAttribute("photo_path", "images/temp/" + session.getId()
              + ".jpg");
      response.sendRedirect("cropping");
      return;
  }
  </code></pre>
  
  <p>Where</p>
  
  <pre><code>private void createPhoto(InputStream is, String outputpath) {
      FileOutputStream os = null;
      try {
          os = new FileOutputStream(outputpath);
          // write bytes taken from uploaded file to target file
          int ch = is.read();
          while (ch != -1) {
              os.write(ch);
              ch = is.read();
          }
      } catch (Exception ex) {
          ex.printStackTrace();
      } finally {
          Helpers.close(os);
      }
  }
  </code></pre>
  
  <p>Now what happens is that the file is uploaded in the StoreLocation (???) on submitting the form so apparently all this  <code>p1.getInputStream()</code> is for naught.</p>
  
  <p>My questions are :</p>
  
  <ul>
  <li>what is StoreLocation ? How tmp are those glassfish uploads ? Where are all those parameters set ? I did read BalusC' <a href="https://stackoverflow.com/a/2424824/281545">tutorial</a> - but there is no mention of StoreLocation (google is not very helpful <a href="https://www.google.com/search?ie=UTF-8&amp;oe=UTF-8&amp;sourceid=navclient&amp;gfns=1&amp;q=StoreLocation%20glassfish" rel="nofollow noreferrer">either</a>).</li>
  <li>What would be a more professional way of handling the situation - including keeping the photos outside the webroot - but using facilities glassfish provides (if it does provide) ?</li>
  <li>Even p1 printing so nice escapes me (it does not <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/Part.html" rel="nofollow noreferrer">seem</a> to Override <code>toString()</code>)</li>
  </ul>
  
  <p>Interested in tips even in how should one rename the photos etc (is this sessionID thing Right ? - check also the time trick) :</p>
  
  <pre><code>if (request.getParameter("save") != null) {
      long time = System.currentTimeMillis();
      String path = "images/upload/" + session.getId() + time + ".jpg";
      String outputpath = this.getServletContext().getRealPath(path);
      // store photo
      InputStream is = p1.getInputStream();
      createPhoto(is, outputpath);
      // etc
  }
  </code></pre>