0

在使用 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)

支持豆

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> 
4

2 回答 2

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}"/>  

在你的支持 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");
    }  

希望能帮助到你

来源

嗨对不起,回复晚了。

请检查您是否在 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 点。

于 2013-08-14T04:28:07.560 回答
1

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

于 2013-08-28T02:09:41.387 回答