2

我正在使用 PrimeFaces <p:fileUpload>。它不调用侦听器方法。如果我添加FileUploadFilter,那么我会得到一个例外。

看法:

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

豆:

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

1 回答 1

0

fileUploadListener="#{fileUploadController.upload()}"这里有问题。我复制了它,我也得到了一个异常方法未找到:

您应该定义不带括号的 fileUploadListener。添加括号后,bean中的预期方法是upload()而不是upload(FileUploadEvent event)

于 2013-02-20T09:29:48.737 回答