本文介绍了在使用Primefaces 3.5文件上传时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



错误

  org.primefaces.component.file b $ b在org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:47)
在javax.faces.component.UIComponentBase.decode(UIComponentBase.java:836)$ b $在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)$ b $ 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)
在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:344)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)

在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。
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
在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)
在org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
在org.glassfish.grizzly .http.server.HttpHandler.doHandle(HttpHandler.java:168)在org.glassfish.grizzly.http.server.HttpServerFilter.handleRead
(HttpServerFilter.java:189)
在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119)
。在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
。在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
处org.glassfish org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
。 (org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
(org.glassfish.grizzly.strategies)中的grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
。 AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerTh readIOStrategy.java:115)
。在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:55)$ B $在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run B(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(uploadloading );
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 // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
xmlns:h =http://java.sun.com/jsf/html
xmlns:p =http://primefaces.org/ui>
< h:head>
< title> Facelet标题< / title>
< h:outputStylesheet library =cssname =maincss.css/>
< / h:头>
< h:body>

< h:form enctype =multipart / form-data>
allowTypes =/(\。| \ / )(gif | jpe?g | png)$ //>
< p:commandButton ajax =falsevalue =提交/>

< / h:表格>


< / h:body>
< / html>

web.xml文件

 <滤光器> 
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< filter-class>
org.primefaces.webapp.filter.FileUploadFilter
< / filter-class>
< init-param>
< param-name> uploadDirectory< / param-name>
<参数值>< /参数值>
< / init-param>
< / filter>

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


解决方案

3.3到3.5



但是最终以第二个解决方案给出下面的结果

第一个解决方案



在primefaces高级文件上传中存在一些问题我认为 code> mode =simple是 p中的问题:fileUpload 标记

使 mode =advanced并测试一次。 / p>

你正在给mode =simple并尝试以高级方式处理。

第二个解决方案
让你的文件上传像下面一样简单处理

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

< p:commandButton value =Submitajax =falseactionListener =#{staffController.upload}/>

而在你的Backing Bean / Controller / staffController中

创建一个属性

 私有的UploadedFile文件; 

// getter setters

把一个 em>方法如下

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

$ / code>

希望有帮助



对不起,迟到的回应

请检查您是否已经为servlet添加了文件上传过滤器web.xml。



1)

 <滤光器> 
< 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><! - 您的FacesServlet名称 - >
< / filter-mapping>





3) $ b

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

 < h:form enctype =multipart /格式数据> <! -  enctype标记很重要 - > 

请检查3分。


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>
解决方案

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 Solutionmake 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 .

这篇关于在使用Primefaces 3.5文件上传时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:48