使用Primefaces 3.5文件上载时出错

时间:2013-08-11 15:21:09

标签: jsf file-upload file-io primefaces

在使用PrimeFaces 3.3的项目中,文件上传功能正常。我删除了Primefaces 3.3并添加了3.5。从那时起,我无法正常上传文件。之前我已经添加了commons-io和commons.fileupload,我不确定它们是否仍然需要,所以我也保留了这些jar文件。

错误

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)

支持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页面

<?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文件

<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 个答案:

答案 0 :(得分:4)

当版本从3.3更改为3.5

时,我也遇到了同样的问题

但最终得到了下面给出的第二个解决方案

第一个解决方案

primefaces高级文件上传存在一些问题

我认为mode="simple"p:fileUpload代码中的问题

制作mode="advanced"并测试一次。

如果有效,那么

问题是你提供mode =“simple”并试图以先进的方式处理。

第二个解决方案 使你的文件上传如下所示以简单的方式处理

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

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

在你的Backing Bean / Controller / staffController中

创建一个属性

    private UploadedFile file;

//getter setters

并按照下面的上传方法

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");
    }  

希望有所帮助

Sources

嗨很抱歉迟到的回复。

请检查您是否在web.xml中添加了servlet的文件上传过滤器。

<强> 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)

在lib文件夹中添加commons-fileupload1.3.jar文件。

第3)

确保您的表单是否具有enctype属性

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

请检查3点。

答案 1 :(得分:1)

导致文件上传错误的事件不是更新primefaces,而是GlassFish从3.1更新到4.0。当我在GlassFish 3.1中简单地部署相同的应用程序时,一切正常。感谢