1
votes

I'm trying to upload a file with a simple form but it just returns to the same page all the time. To find the mistake or whatever is wrong, I just want to write in the output the name of the file I upload. Once I can get the file name I guess I can handle how to handle the entire file. So this is my code:

<h:form enctype="multipart/form-data">    
            <h:outputText value="Argazkia: "/>
            <p:fileUpload value="#{jokoBerriaController.file}" mode="simple"/>
            <p:commandButton value="Bidali" ajax="false" actionListener="#{jokoBerriaController.upload()}"/>
</h:form>

The controller:

import javax.faces.bean.ManagedBean;
import org.primefaces.model.UploadedFile;

@ManagedBean
public class jokoBerriaController {  

    public static UploadedFile file; 

    public UploadedFile getFile() {  
        return file;  
    }  

    public void setFile(UploadedFile file) { 
        this.file = file;  
    }  

    public void upload() {          
        System.out.println("file " + file.getFileName());
    }  
}

I had added

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

at my web.xml file, but i've haven't added commons-io and commons-fileupload libraries. I've read that i have to put the next code in my pom.xml file, but I can't find that file.

<dependency> 
<groupId>commons-fileupload</groupId> 
<artifactId>commons-fileupload</artifactId> 
<version>1.2.2</version> 
</dependency> 
<dependency> 
<groupId>commons-io</groupId> 
<artifactId>commons-io</artifactId> 
<version>1.4</version> 
</dependency>

Thanks for your help.

PD: Sorry about my English.

4
The attribute "allowTypes" restrict the file types. Also see the other attributes for <p:fileUpload>. <p:fileUpload fileUploadListener="#{fileUploadController.upload}" 12. 6.allowTypes="/(\.|\/)(doc|docx|xls|xlsx|zip|gif|jpeg|png)$/" sizeLimit="100000" />Sree Rama

4 Answers

1
votes

If you cannot find the pom.xml then you are not building the application using maven and so you can just copy the downloaded jar files to your WEB-INF/lib folder. Download the commons-fileupload-1.2.2.jar and commons-io-1.4 and add it to that folder.

1
votes

This post was really helpful.

You need to pay attention to all the details. All dependencies must be included in WEB-INF/lib, and the servlet filter in web.xml must be declared correctly.

I had similar issues and I got it working by following the tips on that post.

0
votes

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

to your web.xml file and add commons-io and commons-fileupload to your library path ?

0
votes

You should really add commons-io and commons-fileupload libraries. You download them from the following links as .jar files. Then you need to add the jar files to your project.

http://commons.apache.org/io/

http://commons.apache.org/fileupload/

After adding those jar files into your project, you should be able to get fileupload working.