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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值