2
我正在使用PrimeFaces <p:fileUpload>
。它不调用侦听器方法。如果我添加FileUploadFilter
,那么我会得到一个异常。PrimeFaces uploadFile不起作用
查看:
<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>
签出本教程:http://www.java-tutorial.ch/java-server-faces/file-upload -with-primefaces – Ioan 2013-02-20 09:13:45
你会得到什么例外? “Faces Servlet”是您的jsf servlet的名称吗?否则必须调整以下行: <滤波器映射> PrimeFaces文件上传过滤过滤器名称> 面临的Servlet servlet的名称> 过滤器映射> 成 <滤波器映射> PrimeFaces文件上传过滤过滤器名称> 你的servlet名称该处 servlet的名称> 过滤器映射> –
mooonli
2013-02-20 09:20:34
面孔Servlet是不是名字对于我的jsf servlet, – PyaePhyoAung 2013-02-20 10:12:48