当前位置: 代码迷 >> Java Web开发 >> 又遇乱码有关问题. URLEncoder.encode(input, "UTF-8")
  详细解决方案

又遇乱码有关问题. URLEncoder.encode(input, "UTF-8")

热度:744   发布时间:2016-04-17 13:14:56.0
又遇乱码问题. URLEncoder.encode(input, "UTF-8") ;
1.   客户端对中文使用了urlencode     URLEncoder.encode( "input ",   "UTF-8 ")   ;
2.   服务器端为tomcat   ,spring   mvc框架
      <filter>
<filter-name> CharacterEncodingFilter </filter-name>
<filter-class>
                                      org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> GBK </param-value>
</init-param>
</filter>
3.   服务器端如何才能正确获得中文信息?

目前用了个笨办法,
URLDecoder.decode(   req.getQueryString(), "UTF-8 "   )   ,然后从中解析数据.
不知是否还有其他解决方案.




------解决方案--------------------
可以继承父类,加上一句request.setcharencoding( "gbk ");
super.父类的方法
------解决方案--------------------
楼上注意大小写
request.setCharacterEncoding( "gb2312 ");
而且要写在程序的开头

这样写就不用逐一解析了
------解决方案--------------------
前提条件,每个页面使用
<%@ page contentType= "text/html; charset=GBK " language= "java " import= "java.sql.* " errorPage= " " %>
<meta http-equiv= "Content-Type " content= "text/html; charset=GBK ">
这个你肯定已经写了

1.添加过滤器
在TOMCAT中找到这2个文件RequestDumperFilter.java,SetCharacterEncodingFilter.java,他们位于%Tomcat%\webapps\jsp-examples\WEB-INF\classes\filters,加到你的工程文件里去,编译他们。

2.配置WEB.XML
在web.xml里加入这一段
……
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> filters.SetCharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> GBK </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
……

3.修改server.xml
在server.xml修改2个地方
<Connector port= "8080 "
maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
debug= "0 " connectionTimeout= "20000 "
disableUploadTimeout= "true " URIEncoding= 'GBK '/>
<Connector className= "org.apache.coyote.tomcat5.CoyoteConnector "
port= "8009 " minProcessors= "5 " maxProcessors= "75 "
enableLookups= "true " redirectPort= "8443 "
acceptCount= "10 " debug= "0 " connectionTimeout= "0 "
useURIValidationHack= "false " protocol= "AJP/1.3 "
protocolHandlerClassName= "org.apache.jk.server.JkCoyoteHandler "
URIEncoding= 'GBK '/>

以上是网上参考的,试试吧
不过比较麻烦
个人觉得,还是我前面的那种方法比较简单方便


  相关解决方案