最近在做一个基于Primefaces 3.4.2+EJB的项目,项目中用到了Primefaces作为前端,个人觉得Primefaces可能还处于"完善阶段",小的bug非常的多,在做文件上传的相关模块时用了很多的时间去调试文件上传时"文件为空"的问题,下面对文件的上传与下载做一个完整的总结,具体步骤如下:
(1)JSF Bean
public class FileUploadController { private UploadedFile uploadedFile; public FileUploadController() { } public UploadedFile getUploadedFile() { return uploadedFile; } public void setUploadedFile(UploadedFile uploadedFile) { this.uploadedFile = uploadedFile; } public void submit() { // Get information you from the uploaded file System.out.println("Uploaded file name : " + uploadedFile.getFileName()); } }
(2)JSF page:关于页面有两点需要注意1,form
必须包含"enctype"属性
2 h:commandButton
必须有ajax="false"属性
<h:form id="welcomeForm" enctype="multipart/form-data"
>
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" ajax="false" />
<h:message for="welcomeForm" />
</h:form>
(3)JSF 配置
基本配置如下:
<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>
以下的属性默认的情况下不需要配置
thresholdSize
specifies the maximum file size in bytes to keep uploaded files in memory. If it
exceeds this limit, it’ll be temporarily written to disk.
uploadDirectory is the folder where to keep temporary files that exceed thresholdSize.
<filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> <init-param> <param-name>thresholdSize</param-name> <param-value>51200</param-value> </init-param> <init-param> <param-name>uploadDirectory</param-name> <param-value>C:\etc</param-value> </init-param> </filter>
(4)相关的JAR
添加 commons-fileupload-1.x.x.jar 以及commons-io-2.x.jar
到WEB-INF/lib/下
作者:sun7545526 发表于2013-2-4 23:10:49 原文链接
阅读:37 评论:0 查看评论