我是靠谱客的博主 魁梧季节,这篇文章主要介绍JSF 2.0(基于Primefaces 3.4.2)文件上传与下载的总结,现在分享给大家,希望可以做个参考。


最近在做一个基于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/

最后

以上就是魁梧季节最近收集整理的关于JSF 2.0(基于Primefaces 3.4.2)文件上传与下载的总结的全部内容,更多相关JSF内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(158)

评论列表共有 0 条评论

立即
投稿
返回
顶部