Error while using Primefaces 3.5 File Upload

2019-09-06 12:12发布

In a project with PrimeFaces 3.3, file upload was functioning properly. I removed the Primefaces 3.3 and added 3.5. Since then, I can not function the file upload. Earlier I have added commons-io and commons.fileupload, I was not sure they are still needed, so that I keep these jar files as well.

Error

java.lang.NullPointerException
    at org.primefaces.component.fileupload.FileUploadRenderer.decodeSimple(FileUploadRenderer.java:56)
    at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:47)
    at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:836)
    at javax.faces.component.UIInput.decode(UIInput.java:771)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1228)
    at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
    at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1223)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1223)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:77)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:724)

Backing Bean

public void handleFileUpload1(FileUploadEvent event) { 
    System.out.println("uploading");
    FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);  
    System.out.println("uploaf finished");
} 

JSF page

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
        <h:outputStylesheet library="css" name="maincss.css" />
    </h:head>
    <h:body>

        <h:form enctype="multipart/form-data">
            <p:fileUpload  fileUploadListener="#{staffController.handleFileUpload1}" mode="simple" value="#{staffController.file}"
                           allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />  
            <p:commandButton ajax="false" value="Submit" />

        </h:form>


    </h:body>
</html>

web.xml file

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>
        org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value></param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping> 

2条回答
老娘就宠你
2楼-- · 2019-09-06 13:01

I also faced the same problem earlier when version changed from 3.3 to 3.5

but end up with 2nd solution given below finally

1st Solution

There is some problem in primefaces advanced file upload

I think mode="simple" is the problem in the p:fileUpload tag

Make mode="advanced" and test it once.

If it works ,fine then

The thing is you are giving mode="simple" and trying to handle in advanced way.

2nd Solution make your fileupload like below to handle in a simple manner

<p:fileUpload value="#{staffController.file}" mode="simple"/>  

    <p:commandButton value="Submit" ajax="false" actionListener="#{staffController.upload}"/>  

And in your Backing Bean/Controller/staffController

create a property

    private UploadedFile file;

//getter setters

And put an upload method like below

public void upload() {
        System.out.println("uploading");
        if(file != null) {  
            FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");  
            FacesContext.getCurrentInstance().addMessage(null, msg);  
        }
        System.out.println("uploaf finished");
    }  

Hope it helps

Sources

Hi Sorry for the late response.

Please check whether you have added file upload filter for the servlet or not in your web.xml.

1)

<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><!-- your FacesServlet Name -->
</filter-mapping>

2)

Add commons-fileupload1.3.jar file in lib folder.

3)

Make sure your whether your form has the enctype attribute or not

<h:form enctype="multipart/form-data">   <!-- enctype tag is important -->

Please check the abouve 3 points .

查看更多
放我归山
3楼-- · 2019-09-06 13:11

The event that caused file upload error was not updating primefaces, but GlassFish update from 3.1 to 4.0. When I simply deploy the same application back in GlassFish 3.1, everything worked fine. Thanks

查看更多
登录 后发表回答