我正在尝试使用trinidad fileupload(tr:inputFile)组件将文件上传到服务器,但我得到了
setCharacterEncoding的NullPointerException(UploadRequestWrapper.java:83)
按下commandButton。
我的 XHTML文件包含:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:tr="http://myfaces.apache.org/trinidad" >
<ui:composition template="/WEB-INF/templates/BasicTemplate.xhtml">
<ui:define name="content">
<tr:form usesUpload="true">
<tr:inputFile label="Upload:" valueChangeListener="#{myBackingBean.fileUploaded}"/>
<tr:commandButton text="Begin"/>
</tr:form>
</ui:define>
</ui:define>
</ui:composition>
</html>
支持Bean:
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import org.apache.myfaces.trinidad.model.UploadedFile;
public class MyBackingBean{
...
public void fileUploaded(ValueChangeEvent event){
UploadedFile file = (UploadedFile) event.getNewValue();
if (file != null){
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(
"Successfully uploaded file " + file.getFilename() +
" (" + file.getLength() + " bytes)");
context.addMessage(event.getComponent().getClientId(context), message);
// Here's where we could call file.getInputStream()
}
}
}
WEB-INF / web.xml文件:
<filter>
<filter-name>trinidad</filter-name>
<filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class
</filter>
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<filter-mapping>
<filter-name>trinidad</filter-name>
<servlet-name>faces</servlet-name>
</filter-mapping>
我正在使用:
完整输出:
Schwerwiegend: Servlet.service() for servlet [Faces Servlet] in context with path [/com.cargarantie.ws.claimshandler] threw exception [null] with root cause java.lang.NullPointerException
at org.apache.myfaces.trinidadinternal.config.upload.UploadRequestWrapper.setCharacterEncoding(UploadRequestWrapper.java:83)
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.setRequestCharacterEncoding(ServletExternalContextImpl.java:581)
at javax.faces.context.ExternalContextWrapper.setRequestCharacterEncoding(ExternalContextWrapper.java:416)
at javax.faces.context.ExternalContextWrapper.setRequestCharacterEncoding(ExternalContextWrapper.java:416)
at javax.faces.application.ViewHandler.initView(ViewHandler.java:339)
at org.apache.myfaces.application.ViewHandlerImpl.initView(ViewHandlerImpl.java:191)
at javax.faces.application.ViewHandlerWrapper.initView(ViewHandlerWrapper.java:49)
at org.apache.myfaces.lifecycle.RestoreViewExecutor.doPrePhaseActions(RestoreViewExecutor.java:83)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:182)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:143)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
有什么想法吗?提前谢谢。
答案 0 :(得分:0)
我解决了这个问题!
我不知道这种方法是否是解决问题的最佳方法,但它有效。
创建自己的SetCharacterEncodingFilter类并将其添加到构建路径中。 Source:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
@Override
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String characterEncoding = selectEncoding(request);
if (characterEncoding != null)
request.setCharacterEncoding(characterEncoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
Utilities.ConsoleMsg("Done.", this, "doFilter", 5);
}
@Override
public void init(FilterConfig fConfig) throws ServletException {
this.filterConfig = fConfig;
this.encoding = fConfig.getInitParameter("encoding");
String value = fConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
在web.xml中注册Filter并设置init参数。
<filter>
<filter-name>UTFEncodingFilter</filter-name>
<filter-class>path.to.your.package.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>false</param-value>
</init-param>
</filter>
将URIEncoding="UTF-8"
属性添加到server.xml
中的所有连接器。例如:
<Connector port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
<强>来源:强>