2

I'm using PrimeFaces <p:fileUpload>. It does not invoke the listener method. If I add the FileUploadFilter, then I get an exception.

View:

<h:form enctype="multipart/form-data">
    <p:fileUpload mode="advanced"
        fileUploadListener="#{fileUploadController.upload()}"
        allowTypes="/(\.|\/)(gif|jpg|jpeg|gif|png|PNG|GIF|JPG|JPEG)$/"
        auto="false" />
</h:form>

Bean:

public class fileUploadController {

    private String destination = "c:\test";

    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
        try {
            copyFile(event.getFile().getFileName(), event.getFile()
                    .getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void copyFile(String fileName, InputStream in) {
        try {

            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(destination
                    + fileName));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            in.close();
            out.flush();
            out.close();

            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

web.xml

<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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Checkout this tutorial : http://www.java-tutorial.ch/java-server-faces/file-upload-with-primefaces – Ioan Feb 20 '13 at 09:13
  • what's the exception you get? Is "Faces Servlet" the name of your jsf servlet? Otherwise you have to adjust the following line: PrimeFaces FileUpload Filter Faces Servlet into PrimeFaces FileUpload Filter NAME OF YOUR SERVLET HERE – mooonli Feb 20 '13 at 09:20
  • Faces Servlet is not the name for my jsf servlet, – PyaePhyoAung Feb 20 '13 at 10:12
  • In future questions, always copypaste the entire exception. The exception is basically the whole answer to your problem. You should not ignore exceptions. If you're unable to understand what the exception is telling you, then you should just copypaste it so that we can explain it for you. – BalusC Feb 20 '13 at 12:14
  • You have a fix http://stackoverflow.com/questions/19262356/file-upload-doesnt-work-with-ajax-in-primefaces-4-0-running-on-jsf-2-2-x/19381134#19381134 – DougMH Jun 08 '14 at 18:11

1 Answers1

0

fileUploadListener="#{fileUploadController.upload()}" is problem here. I reproduced that and I also got an exception Method not found:

You should define fileUploadListener whithout parentheses. When you have added parentheses, expected method in your bean is upload() and not upload(FileUploadEvent event)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
miroslav_mijajlovic
  • 1,703
  • 17
  • 31