title: PrimeFaces image upload
body: <p>I am trying to save the image file but the call is not going to the fileHandler method. We have already added filter to the web.xml as mentioned in the primefaces document. This <a href="http://www.primefaces.org/showcase/ui/fileUploadAuto.jsf" rel="nofollow noreferrer">demo</a> I am trying. I also have commons-io and commons-fileupload. How can I correct it? </p>
  
  <h2>XHTML page</h2>
  
  <pre><code>&lt;h:form enctype="multipart/form-data"&gt;
  &lt;p:fileUpload
  fileUploadListener="#{fileUploadController.handleFileUpload}"
  mode="advanced" update="messages" label="Choose a file"
  auto="true" sizeLimit="10485760"
  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
  invalidSizeMessage="The maximum file size allowed is 10 Megabytes !"
  invalidFileMessage="You are allowed to upload only images !" /&gt;
  &lt;p:growl id="messages" showDetail="true" life="5000" /&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <h2>Bean</h2>
  
  <pre><code>@ManagedBean(name = "topicUploadBean")
  @RequestScoped
  public class TopicUploadBean {
  
      //Primitives
      private static final int BUFFER_SIZE = 6124;
  
  
      /** Create a new instance of UploadBean */
      public TopicUploadBean(){
          System.out.println("created FileUploadController instance");
  
      }
  
      public void handleFileUpload(FileUploadEvent event){
          ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
  
          File result = new File(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
          System.out.println(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
          try {
              FileOutputStream fileOutputStream = new FileOutputStream(result);
              byte[] buffer = new byte[BUFFER_SIZE];
              int bulk;
              InputStream inputStream = event.getFile().getInputstream();
              while(true){
                  bulk = inputStream.read(buffer);
                  if (bulk &lt; 0){
                      break;
                  }
                  fileOutputStream.write(buffer,0,bulk);
                  fileOutputStream.flush();
              }
              fileOutputStream.close();
              inputStream.close();
              FacesMessage msg = new FacesMessage("File Description", "file name: " + event.getFile().getFileName() +" File size: "+ event.getFile().getSize()/1024 +" Kb Content type: "+ event.getFile().getContentType() + " the file was uploaded.");
          } catch(IOException e){
              e.printStackTrace();
              FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, " the files were not uploaded!", "");
              FacesContext.getCurrentInstance().addMessage(null, error);
          }
      }
  </code></pre>
  
  <h2>Web.xml</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="WebApp_ID" version="3.0"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
      &lt;display-name&gt;emyed-web&lt;/display-name&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.FACELETS_REFRESH_PERIOD&lt;/param-name&gt;
          &lt;param-value&gt;1&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.FACELETS_SKIP_COMMENTS&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
          &lt;param-value&gt;Production&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.SEPARATOR_CHAR&lt;/param-name&gt;
          &lt;param-value&gt;_&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
          &lt;param-value&gt;server&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.VALIDATE_EMPTY_FIELDS&lt;/param-name&gt;
          &lt;param-value&gt;false&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;primefaces.THEME&lt;/param-name&gt;
          &lt;param-value&gt;#{userPreferences.theme}&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;com.sun.faces.allowTextChildren&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;!-- Custom filter to check for user login --&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;EmyEdAccessFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.zreflect.emyed.filter.EmyEdAccessFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;!-- Filter for uploading files --&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.primefaces.webapp.filter.FileUploadFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;thresholdSize&lt;/param-name&gt;
              &lt;param-value&gt;10240&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;/filter-mapping&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;EmyEdAccessFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/pages/*&lt;/url-pattern&gt;
          &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
      &lt;/filter-mapping&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;!--servlet&gt; &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;com.icesoft.faces.webapp.CompatResourceServlet&lt;/servlet-class&gt; 
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;ImageRenderServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.zreflect.emyed.servlets.ImageRenderServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;DownloadAttachmentServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.zreflect.emyed.servlets.DownloadAttachmentServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  
      &lt;!-- Atmos newly added --&gt;
      &lt;servlet&gt;
          &lt;description&gt;MeteorServlet&lt;/description&gt;
          &lt;servlet-name&gt;MeteorServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.atmosphere.cpr.MeteorServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;org.atmosphere.servlet&lt;/param-name&gt;
              &lt;param-value&gt;com.zreflect.emyed.managedbean.circle.MeteorAtmosphereSharePostServlet&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;org.atmosphere.useNative&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;0&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;MeteorServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/meteor&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
  
      &lt;!-- Atmos done --&gt;
  
      &lt;!--servlet&gt; &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;org.primefaces.resource.ResourceServlet&lt;/servlet-class&gt; 
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Resource 
          Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/primefaces_resource/*&lt;/url-pattern&gt; 
          &lt;/servlet-mapping --&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;!--url-pattern&gt;*.iface&lt;/url-pattern --&gt;
          &lt;url-pattern&gt;*.jspx&lt;/url-pattern&gt;
          &lt;url-pattern&gt;/xmlhttp/*&lt;/url-pattern&gt;
          &lt;url-pattern&gt;*.faces&lt;/url-pattern&gt;
          &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt;
          &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;ImageRenderServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/photo/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;DownloadAttachmentServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/download/attachment/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
      &lt;error-page&gt;
          &lt;error-code&gt;500&lt;/error-code&gt;
          &lt;location&gt;/errors/500.xhtml&lt;/location&gt;
      &lt;/error-page&gt;
      &lt;error-page&gt;
          &lt;exception-type&gt;java.lang.Throwable&lt;/exception-type&gt;
          &lt;location&gt;/errors/500.xhtml&lt;/location&gt;
      &lt;/error-page&gt;
      &lt;!-- error-page&gt; &lt;exception-type&gt;javax.faces.application.ViewExpiredException&lt;/exception-type&gt; 
          &lt;location&gt;/errors/viewExpired.xhtml&lt;/location&gt; &lt;/error-page --&gt;
  &lt;/web-app&gt;
  </code></pre>