I'm trying to get working like what is documented in the primefaces user guide, and some posts founded there.
Upload file in JSF primefaces.
the environnement is : javaee full + jpa + jsf 2.2 + primefaces 4 + glassfish v4
I m posting again, because i have tried every sample and suggestion i have found on the web, without success.
I m able, to get the file uploaded name with : event.getFile.getFileName, but the content is always null
-------------Updates----------------------------
My xhtml page :
<h:form enctype="multipart/form-data">
<p:outputLabel value="La photo :"/>
<p:fileUpload fileUploadListener="#{personController.upload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
my managed bean :
@Named(value = "personController")
@SessionScoped
public class PersonController implements Serializable {
/**
* Creates a new instance of PersonController
*/
@Inject
private PersonneFacade personneService;
private Personne current;
private Personne newPerson;
private List<Personne> personnes;
public PersonController() {
}
public List<Personne> getAll(){
return personneService.findAll();
}
public void upload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
// Do what you want with the file
System.out.println(event.getFile().getFileName());
System.out.println("le fichier " + event.getFile().getContents());
newPerson.setPhoto(event.getFile().getContents());
}
my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- ############################################# -->
<!-- # File upload # -->
<!-- ############################################# -->
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
sure i have on my pom.xml :
<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
when i m triyng to upload i got on the glassfish output log:
INFO: mdmx.jpg
INFO: le fichier null
any idea about ??
null
which causes the NPE. If you have got the correct permissions setup (chmod & user/group rights) for/opt/upld
then it's hard to guess. – Gardener