14

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
DesireToUpload
  • 141
  • 1
  • 1
  • 3
  • 1
    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 Dec 08 '10 at 14:42
  • 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 Dec 08 '10 at 14:48
  • 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 do – DesireToUpload Dec 08 '10 at 14:50
  • Hint: to reply on comments of others on posts which are not their own, use `@nickname` to auto-notify them. See also http://meta.stackexchange.com/questions/43019/how-do-comment-replies-work As to my `ViewExpiredException` problem, it might be solved in 2.2 RC2. Haven't tried yet. Let me know if it works. – BalusC Dec 08 '10 at 17:21
  • @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 case – Thang Pham Jan 07 '11 at 11:25
  • In other words, your problem is still not solved? How about the exception you got in the logs? – BalusC Jan 07 '11 at 11:27
  • @BalusC: Sadly, i see no exception on my server of GF3.0.1. I got `org.primefaces.webapp.filter.FileUploadFilter` inside my web.xml. I got `commons-io-1.4/2.0` and `commons-fileupload-1.2.1/1.2.2`. When I try to upload, debugger does not stop, logger record nothing showing that I have got inside the method that hand fileUploadEvent. Would u kindly and test the showcase for me. I test the showcase on both FF and chrome. When I upload, only the animation run, but growl message does not appear – Thang Pham Jan 07 '11 at 11:35
  • Do you have any filters in the `web.xml` which might be intercepting on `multipart/form-data` requests? – BalusC Jan 07 '11 at 11:39
  • @BalusC: only `PrimeFaces FileUpload Filter` with `org.primefaces.webapp.filter.FileUploadFilter`. That is the only filter I have, and from my understand, we need that filter for primefaces upload component. I might be wrong, though. Does your article about upload with JSF2.0 is ajax upload? – Thang Pham Jan 07 '11 at 11:43
  • @BalusC, @DesireToUpload: oh oh actually, I think what I have done wrong here. When I create `PrimeFaces FileUpload Filter` I did not create a `filter-mapping` for it. So now I map the filter onto `Faces Servlet` and it work. uhmm :) Not sure if this can help `DesireToUpload` since I know nothing about Tomcat. Not sure if it is the same – Thang Pham Jan 07 '11 at 11:51

6 Answers6

19

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That user's guide link doesn't work... do you know any other means of obtaining the apparently very closely guarded working draft of the PrimeFaces 3 user's guide? – Kevin Pauli Aug 08 '11 at 14:54
3

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>
Kijewski
  • 25,517
  • 12
  • 101
  • 143
Rodrigo
  • 31
  • 1
3

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.

Sabin Chirila
  • 779
  • 6
  • 8
2

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).

Roberto de Santis
  • 868
  • 5
  • 15
  • 26
2

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

DMEx38
  • 156
  • 5
1

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?

SkyBlackHawk
  • 95
  • 10