PrimeFaces上传文件不起作用



我使用的是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>

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>

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

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

相关内容

  • 没有找到相关文章

最新更新