fileUpload中的监听器方法永远不会在primefaces中调用



我正在尝试上传带有primefaces的图像,并且未调用fileUploadListener

<h:form enctype="multipart/form-data">
        <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
        mode="advanced" 
        update="messages"
        sizeLimit="100000" 
        allowTypes="/(.|/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>

这是bean:

@ManagedBean
@RequestScoped
public class FileUploadController {
    public void handleFileUpload(FileUploadEvent event) throws Exception {
      System.out.println("OOOOOOOOOOOOOOOOOOK");
            File targetFolder = new File("C:/Uploads");
            InputStream inputStream = event.getFile().getInputstream();
            OutputStream out = new FileOutputStream(new File(targetFolder,
                    event.getFile().getFileName()));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            inputStream.close();
            out.flush();
            out.close();  
    }
}

下面是web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
  <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

我还在类路径中添加了commons-fileupload和Commons-io jar。我不明白为什么不调用handleFileUpload

您已经显式地将文件上传过滤器配置为仅侦听FORWARD调度程序。

您需要从过滤器映射中删除以下条目,以便它在默认情况下仅侦听REQUEST调度程序:

<dispatcher>FORWARD</dispatcher>

或者, REQUEST调度程序添加到过滤器映射中,以便它也可以在正常请求上运行:

<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>

FORWARD调度程序仅在调用过滤器之前调用RequestDispatcher#forward()时是必需的。例如,通过一些URL重写解决方案,如PrettyFaces。然而,到目前为止,问题中提供的信息似乎并没有以任何方式表明你正在使用它。


与具体问题无关,由于PrimeFaces文件上传需要Apache Commons IO,您可能需要考虑使用IOUtils#copy()而不是冗长的输入/输出流循环。参见:如何在JSF中保存上传的文件。

相关内容

  • 没有找到相关文章

最新更新