How to save uploaded file in JSF

前端 未结 2 1851
囚心锁ツ
囚心锁ツ 2020-11-22 06:52

I am uploading a file in JSF. I am using Tomahawk\'s for this, but the same question is applicable on e.g. PrimeFaces

2条回答
  •  情书的邮戳
    2020-11-22 07:15

    PrimeFaces uses two file upload decoder for uploading content of p:fileupload

    1. NativeFileUploadDecoder
    2. CommonsFileUploadDecoder

    NativeFileUploadDecoder is the default upload decoder and it requires servlet container 3.0. So if your servlet container is less than 3 then it will not work with you. Also some application servers make a restriction on using multi-part content

    So if you have a problem with the native upload decoder for any reason you have to use the other decoder which is "CommonsFileUploadDecoder"

    CommonsFileUploadDecoder depends on Apache common lib so you have to put the commons-fileupload and commons-io jars in your class-path the version depends on your primefaces version but 1.3 and 2.2 respectively works for me.

    to use CommonsFileUploadDecoder you have to use filter

    
        PrimeFaces FileUpload Filter
        org.primefaces.webapp.filter.FileUploadFilter
    
    
        PrimeFaces FileUpload Filter
        Faces Servlet
    
    

    wait the filter only will not work because inside the filter it checks about the uploader if its not provided and it detect at least JSF 2.2 so it will bypass the request to the default decoder "Native" and then it will not work for your also

    to force the filter to use the common decoder you have to put the following context param in your web.xml

    
    primefaces.UPLOADER
       commons
    
    

提交回复
热议问题