欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Primefaces 3.4.2 "文件上传"的总结

程序员文章站 2022-08-10 16:57:45
(1)jars   添加commons-fileupload-1.2.2.jar and commons-io-1.4.jar到web-inf/lib目录下     (...
(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 

 

 

 

[html]  

<span style="font-size: 14px"><h:form enctype="multipart/form-data">  

    <p:fileupload value="#{filebean.uploadedfile}" mode="simple" />  

    <p:commandbutton value="submit" ajax="false"/>  

</h:form></span>  

 

<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

 

          

 

[java] 

<span style="font-size: 14px">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());  

    }  

  

} </span>  

 

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());

    }

 

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