title: How to get JSF to upload file with Apache Commons FileUpload
body: <p>I know how to do file upload using Primefaces or using Tomahawk, however, I am trying to doing file upload using Apache Commons FileUpload and so far I am having a bit of road block. Even though my form use <code>multipart/form-data</code>, when I submit my form, the content type become <code>application/x-www-form-urlencoded</code>. Here is my code</p>
  
  <pre><code>&lt;h:body&gt;
      &lt;h:form enctype="multipart/form-data"&gt;
          Upload File
          &lt;input type="file" name="file"/&gt;
          &lt;p:commandButton value="Submit" action="#{viewBean.submit}"/&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  </code></pre>
  
  <p>Here is my <code>ViewBean</code></p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class ViewBean implements Serializable {
      public void submit() {
          String url = "/FileUploadServlet";
          FacesContext context = FacesContext.getCurrentInstance();
          try {
              String contentType = context.getExternalContext().getRequestContentType();
              context.getExternalContext().dispatch(url);
          } catch (Exception e) {
              logger.log(Level.SEVERE, "Exception when calling Servlet", e);
          } finally {
              context.responseComplete();
          }
      }
  }
  </code></pre>
  
  <p>So when I try to print the content type above, it showed <code>application/x-www-form-urlencoded</code>. If I put <code>ajax="false"</code> to my <code>p:commandButton</code>, then the <code>submit()</code> method is not even invoked, but if I take out <code>enctype="multipart/form-data"</code> (still keep <code>ajax="false"</code>), then <code>submit()</code> is invoked but it is not multipart, it is <code>application/x-www-form-urlencoded</code>, so apache commons fileupload throw an exception since it is not multipart. Seems like whatever I do, I cant seems to get the multipart requrest. Please help</p>