Richfaces文件上传错误



我尝试使用rich:fileupload组件上传文件。当我试图上传文件时,出现了错误,错误是

Aug 24, 2011 9:41:19 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
        at org.richfaces.renderkit.FileUploadRendererBase.doDecode(FileUploadRendererBase.java:140)
        at org.ajax4jsf.renderkit.RendererBase.decode(RendererBase.java:75)
        at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:764)
        at javax.faces.component.UIInput.decode(UIInput.java:719)
        at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1006)
        at javax.faces.component.UIInput.processDecodes(UIInput.java:633)
        at javax.faces.component.UIForm.processDecodes(UIForm.java:203)
        at org.ajax4jsf.component.AjaxViewRoot$1.invokeContextCallback(AjaxViewRoot.java:392)
        at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:238)
        at org.ajax4jsf.component.AjaxViewRoot.processDecodes(AjaxViewRoot.java:409)
        at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
        at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
        at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:365)
        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:679)

请帮我解决这个问题

stacktrace提示你同时使用了RichFaces 3.3和Tomahawk 1.x。两个组件库都提供文件上传组件,并使用特殊的Filter从请求体中提取上传的文件。stacktrace显示Tomahawk在RichFaces 之前解析请求体

at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:365)
...
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)

一旦请求体被解析,它就不能再被解析了。因此RichFaces无法从请求体中提取上传的文件。你所面临的NullPointerException只是这个不可预见的后果(RichFaces家伙应该抛出一个更明确的异常,而不是像IllegalStateException这样的消息"上传的文件在请求体中丢失",但是ala)。

如果你想使用RichFaces文件上传功能,那么你真的必须从web.xml中删除战斧的ExtensionsFilter。您只会错过<t:inputFileUpload>功能和特定外观组件的一些小特性(脚本/样式表)。战斧的其他部件应该可以正常工作。

RichFaces 3.3文件上传工作正常

Add this in your page:
<rich:fileUpload id="fileupload" addControlLabel="Browse" required="true"
fileUploadListener="#{ demoForm.listener}" acceptedTypes="xml"
ontyperejected="alert('Only xml files are accepted');"
maxFilesQuantity="1" listHeight="57px" listWidth="100%" disabled="#{demoForm.disabled}" >
<a4j:support event="onclear" action="#{ demoForm.clearUploadData}" reRender="fileupload" />
</rich:fileUpload>

FormBean:

/**
*
* @param event
* @throws Exception
*/
public void listener(UploadEvent event) throws Exception{
UploadItem item = event.getUploadItem();
FileUpload file = new FileUpload();
file.setLength(item.getData().length);
file.setFile(item.getData());
file.setName(item.getFileName());
files.add(file);
}

/**
 * 
 * @return
 */public String clearUploadData() {
files.clear();
setUploadsAvailable(1);
return null;
}

将此内容添加到web.xml

<!-- Richfaces  fileupload-->
    <filter>
        <!-- Parameters used by rich file upload -->
        <init-param>
            <param-name>createTempFiles</param-name>
            <param-value>false</param-value>
        </init-param>
        <!-- configurable property of rich:fileUpload to set maximum allowed file size, currently set to  200MB -->
        <init-param>
            <param-name>maxRequestSize</param-name>
            <param-value>209715200</param-value>
        </init-param>
        <init-param>
            <param-name>forceparser</param-name>
            <param-value>false</param-value>
        </init-param>
    </filter>

相关内容

  • 没有找到相关文章

最新更新