0

I've done this part of the form

<td>
   <h:form enctype="multipart/form-data">  
      <p:fileUpload fileUploadListener="#{uploadBean.handleFileUpload(event)}"  
                    mode="advanced"  
                    update="messages"   
                    multiple="true"  
                    sizeLimit="2000000"   
                    allowTypes="/(\.|\/)(pdf|doc?x|xls?x)$/"/>  
       <p:growl id="messages" showDetail="true"/>  
    </h:form>
</td> 

and this event handler in class:

    public class UploadBean {

    /** Creates a new instance of UploadBean */
    public UploadBean() {
    }
    private static final int BUFFER_SIZE = 6124;
    public void handleFileUpload(FileUploadEvent event) {

    ExternalContext extContext = FacesContext.getCurrentInstance().
                                 getExternalContext();
    File result = new File(extContext.getRealPath
     ("//WEB-INF//upload") + "//" + 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 < 0) {
             break;
             }
      fileOutputStream.write(buffer, 0, bulk);
      fileOutputStream.flush();
      }

      fileOutputStream.close();
      inputStream.close();

      FacesMessage msg = new FacesMessage("Succesful", 
          event.getFile().getFileName() + " is uploaded.");
      FacesContext.getCurrentInstance().addMessage(null, msg);

      } catch (IOException e) {

      FacesMessage error = new FacesMessage("The files were not uploaded!");
      FacesContext.getCurrentInstance().addMessage(null, error);
      }
    }
}

Now the handling method I got it from a site. I am not sure why this is failing to upload. it looks okay to me. Maybe am missing something? so the control appears on my page and I can choose file, but then upload progress bar just proceeds fast...no growl notification shows and also no file uploaded of course. Thanks,

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
sys_debug
  • 3,883
  • 17
  • 67
  • 98

1 Answers1

3

From the docs (Assuming using Primefaces)
First thing to do is to configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet.

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

Hope you are not missing this setting.

Also not sure whats event in your code

<p:fileUpload fileUploadListener="#{uploadBean.handleFileUpload(event)}" .....
baba.kabira
  • 3,111
  • 2
  • 26
  • 37
  • Nah I didn't include that in my XML because each time I do it, it crashes. I have no idea why. And I can't find a whole example of how this filter can be added in the XML file. As I said, the code was copied from a site that claims it works...should I remove it? – sys_debug Mar 13 '12 at 10:08
  • I used it as mentioned in PF docs, using PF 3.0, JSF 2.1.4 on tomcat 7.X, it works for me. It crashes what is the error, I guess you will need some apache common libraries too namely commons-io-X.jar,commons-fileupload-X.jar . – baba.kabira Mar 13 '12 at 10:25
  • @sys_debug found @ SO http://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked/8880083#8880083 – baba.kabira Mar 13 '12 at 10:36