我正在使用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>
答案 0 :(得分:0)
fileUploadListener="#{fileUploadController.upload()}"
这里有问题。我重现了这一点,我也得到了一个异常方法:
您应该在没有括号的情况下定义fileUploadListener。添加括号后,bean中的预期方法是upload()而不是upload(FileUploadEvent事件)