问题描述
< h:form enctype = 多部分/格式数据>
< p:fileUpload fileUploadListener =#{fileUploadController.handleFileUpload}
mode =advanced
update =messages
sizeLimit =100000
allowTypes =/(\。| \ /)(gif | jpe?g | png)$ //>
< p:growl id =messagesshowDetail =true/>
这里是bean: / b>
@ManagedBean
@RequestScoped
public class FileUploadController {
public void handleFileUpload(FileUploadEvent event)抛出异常{
System.out.println(OOOOOOOOOOOOOOOOOOOOK);
文件targetFolder =新文件(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]; ((read = inputStream.read(bytes))!= -1){
out.write(bytes,0,read);
while
}
inputStream.close();
out.flush();
out.close();
$ p $这里是web.xml配置:
<?xml version =1.0encoding =UTF-8?>
< web-app version =3.0xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi: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>
<参数值>开发< /参数值>
< / context-param>
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
<加载启动> 1< /加载启动>
< / 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>
我还在classpath中添加了commons-fileupload和Commons-io jar。
我不理解为什么不调用handleFileUpload。
解决方案您已经明确地配置了文件上传过滤器只能在 FORWARD
调度程序上收听。
您需要删除以下条目从过滤器映射,以便它默认侦听 REQUEST
调度程序:
<调度> FORWARD< /调度程序>
或者添加 REQUEST
code> dispatcher添加到过滤器映射中,以便在正常的请求中运行:
< dispatcher>请求< ; /调度程序>
< dispatcher> FORWARD< / dispatcher>
FORWARD
a href =http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#forward%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29 =nofollow noreferrer > RequestDispatcher#forward()
在调用过滤器之前称为。例如,通过一些URL重写解决方案,如PrettyFaces。到目前为止,在问题中提供的信息似乎并没有表明你使用的是什么。
因为PrimeFaces文件上传需要Apache Commons IO,所以你可以考虑使用 IOUtils#copy()
代替那个不相关的详细的输入/输出流循环。另请参阅:。
I am trying to upload an image with primefaces and the fileUploadListener is not invoked.
<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"/>
here is the 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();
}
}
And here is the web.xml configuration:
<?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>
I have also added the commons-fileupload and Commons-io jars in the classpath.I am not understanding why the the handleFileUpload is not invoked.
解决方案 You've explicitly configured the file upload filter to listen on FORWARD
dispatcher only.
You need either to remove the following entry from the filter mapping so that it listens by default on the REQUEST
dispatcher only:
<dispatcher>FORWARD</dispatcher>
Or, to add the REQUEST
dispatcher to the filter mapping so that it would run on normal requests as well:
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
The FORWARD
dispatcher is only mandatory when RequestDispatcher#forward()
is been called before the filter is invoked. For example, by some URL rewriting solution such as PrettyFaces. The information provided so far in the question does however not seem to indicate in any way that you're using that.
Unrelated to the concrete problem, as the PrimeFaces file upload requires Apache Commons IO, you may want to consider to IOUtils#copy()
instead of that verbose input/output stream loop. See also: How to save uploaded file in JSF.
这篇关于p:fileUpload中的监听器方法永远不会在primefaces中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!