【问题标题】:PrimeFaces uploadFile is not workingPrimeFaces 上传文件不起作用
【发布时间】:2013-02-05 05:29:31
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 您遇到什么异常? “Faces Servlet”是您的 jsf servlet 的名称吗?否则,您必须调整以下行: PrimeFaces FileUpload FilterFaces Servlet into PrimeFaces FileUpload Filter您的 SERVLET 的名称
  • Faces Servlet 不是我的 jsf servlet 的名称,
  • 在以后的问题中,始终复制粘贴整个异常。例外基本上是您问题的全部答案。你不应该忽略异常。如果您无法理解异常告诉您什么,那么您应该复制粘贴它,以便我们为您解释。

标签: jsf file-upload primefaces


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 2014-05-15
    • 2013-08-31
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2010-12-27
    相关资源
    最近更新 更多