title: How to send a file from JavaScript to a Java WebService
body: <p>I have an HTML5 application, using Cordova, which you can upload files (images and videos) from your device. And I must send this file uploaded by the user to a Java WebService, and then upload it to a server. </p>
  
  <p>I need help because I'm not able to achieve what I want. I try several solutions found in Internet but without success. </p>
  
  <p>The WeService returns the next <strong>exception</strong>:</p>
  
  <pre><code>[org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null]
  </code></pre>
  
  <p>This is my <strong>code</strong> right now:</p>
  
  <p><strong>HTML</strong>: </p>
  
  <pre><code>&lt;section id="uploadMedia"&gt;
      &lt;input type="file" name="fileMedia" id="fileMedia" &gt;
  &lt;/section&gt;
  </code></pre>
  
  <p><strong>JS</strong>:</p>
  
  <pre><code>var file = $("#uploadMedia").find("#fileMedia")[0].files[0];
  if (typeof file !== "undefined") {
      uploadFile(file);
  }
  
   var uploadFile = function(file, callback) {
          // Create a new FormData object
          var formData = new FormData();
          formData.append('file', file);
  
              $.ajax({
                  url: WEBSERVICE_URL + "uploadFile",
                  beforeSend: function(xhr) {
                      if (WEBSERVICE_USER !== "") {
                          xhr.setRequestHeader("Authorization", "Basic " + btoa(WEBSERVICE_USER + ":" + WEBSERVICE_PASS));
                      }
                  },
                  data: formData,
                  method: "POST",
                  processData: false, // tell jQuery not to process the data
                  contentType: false, // tell jQuery not to set contentType
                  success: function(data, textStatus, jqXHR) {
                      alert(data);
                  },
                  error: function(jqXHR, textStatus, errorThrown) {
                      alert("ERROR");
                  },
                  complete: function(jqXHR, textStatus) {
                      if (typeof callback === "function") {
                          callback();
                      }
                  }
              });
      };
  </code></pre>
  
  <p><strong>JAVA</strong>:</p>
  
  <pre><code>@MultipartConfig
  @WebServlet(name = "uploadFile", urlPatterns = {"/uploadFile"})
  public class UploadFile extends HttpServlet {
  
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
          response.setContentType("application/json;charset=UTF-8");
          try (PrintWriter out = response.getWriter()) {
              String json = "";
  
              Part file = request.getPart("file");
              String filename = "xcvxcv";
              InputStream filecontent = file.getInputStream();
  
              json = "File " + filename + " successfully uploaded";
              out.print(json);                
          }
      }
  }
  </code></pre>
  
  <p>I really appreciate every kind of help.</p>