昨天碰到一个问题:同一个请求页面,页面经过匹配后调用http的post协议接口,部署在linux环境的没问题,本地Eclipse启动的tomcat也没问题,直接启动本地tomcat却报错了:
</pre><p><b>Root Cause</b></p><pre>java.nio.charset.MalformedInputException: Input length = 1
java.nio.charset.CoderResult.throwException(CoderResult.java:281)
org.apache.tomcat.util.buf.B2CConverter.convert(B2CConverter.java:259)
org.apache.catalina.connector.InputBuffer.realReadChars(InputBuffer.java:413)
org.apache.catalina.connector.InputBuffer.checkCharBufferEof(InputBuffer.java:637)
org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:455)
org.apache.catalina.connector.CoyoteReader.read(CoyoteReader.java:108)
com.google.gson.stream.JsonReader.fillBuffer(JsonReader.java:1287)
com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1325)
com.google.gson.stream.JsonReader.consumeNonExecutePrefix(JsonReader.java:1567)
com.google.gson.stream.JsonReader.doPeek(JsonReader.java:534)
com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
com.google.gson.internal.Streams.parse(Streams.java:46)
com.google.gson.JsonParser.parse(JsonParser.java:84)
com.google.protobuf.util.JsonFormat$ParserImpl.merge(JsonFormat.java:1053)
com.google.protobuf.util.JsonFormat$Parser.merge(JsonFormat.java:286)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
经过调试发现匹配后的post请求中存在中文,因此怀疑是中文编码问题。在linux下是utf-8
$ echo $LANG
en_US.UTF-8
Eclipse工具里配置的也是utf-8
![](https://i-blog.csdnimg.cn/blog_migrate/3548e2d08f94f05bd9676c0ac0b7e2f0.png)
而本地tomcat默认是GBK,需要改为utf-8,在tomcat的bin/catalina.bat加入如下设置即可
set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Dfile.encoding=UTF-8"
重新启动tomcat后问题解决