当前位置: 代码迷 >> Java Web开发 >> Servlet偶尔出现java.io.UnsupportedEncodingException: 'utf-8'解决方案
  详细解决方案

Servlet偶尔出现java.io.UnsupportedEncodingException: 'utf-8'解决方案

热度:700   发布时间:2016-04-17 10:49:08.0
Servlet偶尔出现java.io.UnsupportedEncodingException: 'utf-8'
我在C#中调用servlet,这个servlet每天响应几千个请求,但只是几次到10来次报错,我现在连调试都没法做,唉
 
servlet的代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
Connection conn= null;
String cetifyResult =null;
StringBuilder requestData = new StringBuilder();
try {
BufferedReader reqIn = request.getReader(); 按照JAVA报错信息,81行指的就是这句




错误信息为:
java.io.UnsupportedEncodingException: 'utf-8'
at sun.nio.cs.StreamDecoder.forInputStreamReader(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at org.apache.tomcat.util.buf.ReadConvertor.<init>(B2CConverter.java:198)
at org.apache.tomcat.util.buf.B2CConverter.reset(B2CConverter.java:117)
at org.apache.tomcat.util.buf.B2CConverter.<init>(B2CConverter.java:56)
at org.apache.catalina.connector.InputBuffer.setConverter(InputBuffer.java:543)
at org.apache.catalina.connector.InputBuffer.checkConverter(InputBuffer.java:510)
at org.apache.catalina.connector.Request.getReader(Request.java:1152)
at org.apache.catalina.connector.RequestFacade.getReader(RequestFacade.java:470)
at org.ea.test.LicenseCertificationServlet.doPost(LicenseCertificationServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
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.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:774)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:896)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
at java.lang.Thread.run(Unknown Source)



------解决方案--------------------

------解决方案--------------------
在类中每一个方法体中首先加入request.setCharacterEncoding("UTF-8"),然后再写下面的,因为只要牵涉到dopost和doget的都需要转码,所以你第一步需要做的就是转码,然后再写下面的代码。
------解决方案--------------------
搞个filter 转一下码吧

探讨

Java code

ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream in = request.getInputStream();
int bytesRead = 0;
while ((bytesRead = ……
http://www.lunwenwa.com/biyelunwen/

------解决方案--------------------
  相关解决方案