14
votes

I have posted my question on the Primefaces forum but no one has responded so I figured I would try here.

I have been attempting to get fileUpload to work for some time now. I am currently running the RC2 build with mojarra 2.0.3 and Tomcat 7.

I have a dialog which will contain the fileUpload component like so.

<p:dialog id="uploadFileDialog" >
   <h:form id="uplaodFileForm" prependId="false" enctype="multipart/form-data">
       <p:fileUpload fileUploadListener="#{fileUploadController.uploadFile} auto="true"/>    
   </h:form>
</p:dialog>

The fileUploadController looks like this

public class FileUploadController {
    public void uploadFile(FileUploadEvent event) {
         byte[] file = event.getFile().getContents();

         System.out.println("MADE IT INTO FILE UPLOAD !!! ");
    }
}

For some reason when the file is uploaded it never triggers the fileUploadEvent and it never gets into the controller. The upload looks like its working, the flash part renders and gives the impression its doing something but no backing bean is ever being called. I can seem to figure out what I am doing wrong and I have read just about every post on uploading a file using primefaces. Anyone know what I am doing wrong?

6
PF 2.1 file upload doesn't work here on Tomcat 7.0.5 as well. I see ViewExpiredException in the server logs on every upload attempt. PF forums reports the same in several topics: "works on Tomcat 6, but not on Tomcat 7". Do you see anything in the server logs?BalusC
It wasn't showing any exception in the server logs for me. It just shows nothing. But this is not good news. Is there any alternative to this so I can upload files? This is a critical part of my application.DesireToUpload
Actually now that I look into it I get an exception that is this java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream on a side note, I want to thank you for your code on your blog. You have really saved me a tremendous amount of time with past problems I have encountered with your detailed solutions! Thank you very much for what you doDesireToUpload
Hint: to reply on comments of others on posts which are not their own, use @nickname to auto-notify them. See also meta.stackexchange.com/questions/43019/… As to my ViewExpiredException problem, it might be solved in 2.2 RC2. Haven't tried yet. Let me know if it works.BalusC
@BalusC, @DesireToUpload: I am using 2.2RC2 now. Even though I have not encounter ViewExpiredException since I am using Glassfish, fileUploadEvent never fired in my caseThang Pham

6 Answers

19
votes

java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream

PrimeFaces fileupload uses Apache Commons FileUpload under the covers which in turn has another dependency, the Apache Commons IO. Ensure that you've both JAR's in your /WEB-INF/lib.


Update: as per the comments, you also need to ensure that the upload filter is been declared in web.xml as per the users' guide:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

And you also need to ensure that there are no other filters before in the web.xml which may be reading the HttpServletRequest#getInputStream(), because it can be read only once.

3
votes

I have also experienced a similar problem. The fix for me (using a Maven project) was to add the following dependencies in the pom.xml file:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>

This is the equivalent of having the corresponding .jar files in your WEB-INF/lib, so try do that if this is not a Maven project.

3
votes

that is correct you must add

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

AND later this

<!-- JSF mapping -->
<servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

aditionaly if your using maven add this dependencies

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>     
<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-io</artifactId>
     <version>1.3.2</version>
</dependency>
<dependency>
     <groupId>portlet-api</groupId>
     <artifactId>portlet-api</artifactId>
     <version>1.0</version>
</dependency>
2
votes

i think i have solved your problem. Check on your web.xml the presence of:

<context-param>
    <param-name>com.sun.faces.enableViewStateIdRendering</param-name>
    <param-value>false</param-value>
</context-param>

You must delete this option or set it do True (the default value).

2
votes

I have the same issue, I have solved it by adding

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

like BalusC said.

but adding this :

<!-- JSF mapping -->
<servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

Because by default in J2EE 6 this part is optional JSF 2.0 Servlet activates automatically when the WEB-INF/faces-config.xml descriptor is present.

But it necessary to active correctly PrimeFaces Filter

Jboss 6.1.0.Final / PrimeFaces 3.0.RC2

1
votes

In Websphere 7 the event is fired because when I select file and press upload I can see bar to upload that grow up. The problem is that in Websphere 7 I suppose that there are a filter that consume HttpRequest and when arrive to event listener is just consumed so don't have data :(

No message are present in log the debugging is very complicate. Exists some trace or logger to enable in JSF 2 Mojarra 2 and PrimeFaces 3.4.2?