Primefaces 3.4.2 "文件上传"的总结


(1)Jars

添加commons-fileupload-1.2.2.jar and commons-io-1.4.jar到web-inf/lib目录下

(2)web.xml

添加如下配置到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>
(3)*.xhtml


<h:form enctype="multipart/form-data">
    <p:fileUpload value="#{fileBean.uploadedFile}" mode="simple" />
    <p:commandButton value="Submit" ajax="false"/>
</h:form>

Note:(1)Please note the enctype="multipart/form-data" attribute of the form. This is mandatory for HTML in order to be able to send files to the server. The filter is mandatory for JSF in order to extract the data from multipart/form-data requests. Without either of them, either the command action won't be invoked, or all properties will be null.

          (2)simple upload doesn't support ajax.  should add ajax="false" on commondButton

(4)Manager 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());
    }

} 

在写这篇文章时,还有个问题没有解决,就是上传文件的名称为中文时,保存的文件名为乱码,期待完善中.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值