0
votes

I'm trying to upload a file using PrimeFaces, but the fileUploadListener method isn't being invoked after the upload finishes. I add the jar commons-io and commons-fileupload.

I am using

  • glassifish 3.1
  • primafaces 5.0
  • eclipseLink
  • jsf 2.

I searched a lot but nothing worked. This is my code.

The managed Bean :

package image;
import java.io.IOException;
import java.io.InputStream;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;

import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;


@ManagedBean(name="uploadBean")
@SessionScoped
public class UploadBean {

private UploadedFile file;
private Fichier fichier;

@EJB
private FichierDao fichierDao;

public UploadBean()
{
    fichier = new Fichier();
}
public void handleFileUpload(FileUploadEvent event) {
   file= event.getFile();
  // System.out.print("gfghfhgf"+file.getSize());

}
// Store file in the database
public void storeImage() {

    System.out.println("11111mamlamlmalmalmlkljkjkhkjhjhjghj");
    try {
        InputStream is =file.getInputstream() ;
        fichier.setKey(IOUtils.toByteArray(is));
        System.out.println("22222222mamlamlmalmalmlkljkjkhkjhjhjghj");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("33333333333mamlamlmalmalmlkljkjkhkjhjhjghj");
    }
    System.out.println("44444444444mamlamlmalmalmlkljkjkhkjhjhjghj");

    fichierDao.create(fichier);
    System.out.println("55555555555mamlamlmalmalmlkljkjkhkjhjhjghj");
}

public UploadedFile getFile() {
    return file;
}

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

public Fichier getFichier() {
    return fichier;
}   
}

the page xhtml

   <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta charset="utf-8" />
</h:head>
<h:body>
    <h:outputText value="PrimeFaces Single Upload"
        style="font:30px bold; margin-left:15%;" />
    <h:form enctype="multipart/form-data">
        <p:fileUpload
        fileUploadListener="#{uploadBean.handleFileUpload}" ajax="false"
            mode="advanced"
            label="Choisir une image"
            update="messages" 
            sizeLimit="100000" 
            fileLimit="1"
            invalidSizeMessage="La taille maximale du fichier est 1  Megabyte !"
            invalidFileMessage="Charger des images"
            allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
        <p:growl id="messages" showDetail="true" />

     <p:commandButton value="Submit"  action="#{uploadBean.storeImage}" />
    </h:form>

</h:body>
</html>

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <!-- Changer cette valeur à "Production" lors du déploiement final de l'application -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>
    <context-param>
        <param-name>
            javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
        </param-name>
        <param-value>true</param-value>
    </context-param>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>10240</param-value> <!-- 10 Mb -->
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <!-- Déclaration du contrôleur central de JSF : la FacesServlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Mapping : association des requêtes dont le fichier porte l'extension 
        .xhtml à la FacesServlet -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>page1.xhtml</welcome-file>
    </welcome-file-list>
</web-app>`
2
UploadBean should implement Serializable interface - mstzn
still the same problem!! - user3551474
Did you try removing ajax="false" ? - britulin
yes but still the same problem!! - user3551474
Do you can see preview picture or invalid messages after selecting any file? - Rafael Ruiz Tabares

2 Answers

2
votes

Use ajax="false" on any command component that submits the File Upload or File Download form.

Since Upload/Download are HTTP GET requests don't use Ajax submits which are indeed POST requests.

<p:commandButton value="Submit" ajax="false"  action="#{uploadBean.storeImage}" />
0
votes

Here is an example of how to upload a file using primefaces 5.0; and you do not need commons-fileupload-1.3.1.jar and commons-io-2.4.jar; and also you do not need to change web.xml , and declare growl inside h:form and not inside p:fileUpload for more information see this How to upload files in primefaces

The managed Bean:

package image;
    import java.io.IOException;
    import java.io.InputStream;

    import javax.ejb.EJB;
    import javax.enterprise.context.SessionScoped;
    import javax.faces.bean.ManagedBean;

    import org.apache.commons.io.IOUtils;
    import org.primefaces.model.UploadedFile;


    @ManagedBean(name="uploadBean")
    @SessionScoped
    public class UploadBean {

    private UploadedFile file;
    private Fichier fichier;

    @EJB
    private FichierDao fichierDao;

    public UploadBean()
    {
        fichier = new Fichier();
    }

    // Store file in the database
    public void storeImage() {

    if(!(file.getContentType().equalsIgnoreCase("image type"))){ 
        FacesMessage message = new FacesMessage("Not Uploaded","Required file types: ");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }
        if(!(file.getSize() <= 10000000)){ // size
        FacesMessage message = new FacesMessage("Not Uploaded","file size should be less than or equal  to 10mb");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }
        if(file.getSize()==0){
        FacesMessage message = new FacesMessage("","select a file");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }
        if((file.getSize() > 0 && file.getSize() <= 10000000) && (file.getContentType().equalsIgnoreCase("application/vnd.ms-excel")) ){

//your code here for storing image
 System.out.println("11111mamlamlmalmalmlkljkjkhkjhjhjghj");
        try {
            InputStream is =file.getInputstream() ;
            fichier.setKey(IOUtils.toByteArray(is));
            System.out.println("22222222mamlamlmalmalmlkljkjkhkjhjhjghj");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("33333333333mamlamlmalmalmlkljkjkhkjhjhjghj");
        }
        System.out.println("44444444444mamlamlmalmalmlkljkjkhkjhjhjghj");

        fichierDao.create(fichier);
        System.out.println("55555555555mamlamlmalmalmlkljkjkhkjhjhjghj");

//your code above for storing image

       FacesMessage message = new FacesMessage("Succesfull", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }

    }

    public UploadedFile getFile() {
        return file;
    }

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

    public Fichier getFichier() {
        return fichier;
    }   
    }

the page xhmtl:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta charset="utf-8" />
</h:head>
<h:body>
    <h:outputText value="PrimeFaces Single Upload"
        style="font:30px bold; margin-left:15%;" />
    <h:form enctype="multipart/form-data">
 <p:growl id="messages" showDetail="true" />
        <p:fileUpload
       value="#{uploadBean.file}" mode="simple"/>
     <p:commandButton value="Submit"  actionListener="#{uploadBean.storeImage}" 
ajax="false" disabled="false"/>
    </h:form>

</h:body>
</html>