title: Uploading images on Server using Servlet, AJAX and JS
body: <p>Been searching all day and still can't find an answer, so I'm here 
  (I'm new to Servlet programming)</p>
  
  <p>I've got a form like this:</p>
  
  <pre><code>&lt;form onsubmit="return performUpload()"&gt;
   &lt;input type="text" id="upload-name" name="upload-name" required="required" /&gt;
   &lt;input type="text" id="upload-address" name="upload-addres" required="required" /&gt;
   &lt;input type="file" id="upload-file" name="upload-file" required="required" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I want to use the <code>google.maps.Geocode</code> in the JS function before sending the data to <code>/UploadServlet</code> (need to register latitude/longitude on the server)
  I'm trying to use <code>XMLHttpRequest</code> to send the data, something like this</p>
  
  <pre><code>//[some jquery to access form values]
  var formdata=new FormData();
  formdata.append("img",file);
  formdata.append("upload-name",name);
  //[other appends] 
  
  var xhttp=new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
     if (this.readyState == 4 &amp;&amp; this.status == 200) {
      //some code here (alert to confirm upload)
     }
  };  
  xhttp.open("POST","UploadServlet",true);
  xhttp.send(formdata);
  </code></pre>
  
  <p>Now, how can I access all these parameters from the Servlet?
  How can I upload the image on the server and use the other parameters for my needs?</p>
  
  <p>I've found this code on the internet</p>
  
  <pre><code>    protected void doGet(HttpServletRequest request, HttpServletResponse 
        response) throws ServletException, IOException {
  
      //String saveDir = ?
      //here I'd need to use one of the parameters i've passed through the 
       //  ajax request;
  
      // gets absolute path of the web application
      String appPath=getServletContext().getRealPath("/")+"\\images";
  
      // constructs path of the directory to save uploaded file
      String savePath = appPath + File.separator + saveDir;
  
  
      for (Part part : request.getParts()) {
  
          String fileName = extractFileName(part);
          //refines the fileName in case it is an absolute path
          fileName = new File(fileName).getName();
          part.write(savePath + File.separator + fileName);
      }
  }
  
  private String extractFileName(Part part) {
      String contentDisp = part.getHeader("content-disposition");
      String[] items = contentDisp.split(";");
      for (String s : items) {
          if (s.trim().startsWith("filename")) {
              return s.substring(s.indexOf("=") + 2, s.length()-1);
          }
      }
      return "";
  }
  </code></pre>
  
  <p>Which works pretty good and is able to upload the image wherever I want using a form with only one input field of type "file" and without passing through JS/AJAX but using </p>
  
  <pre><code>      &lt;form action=".." &gt;
  </code></pre>
  
  <p>Unfortunately, I can't find a way to "adapt" this code to my needs (still don't understand how <code>request.getParts()</code> works )</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks in advance </p>