zoukankan      html  css  js  c++  java
  • Tomcat7项目迁移到Tomcat8中文乱码问题

    我打算开始使用Tomcat8了,先解决中文乱码问题,在解决其它的问题!
    个人推荐:修改server.xml方式
    对于SpringMVC报的错误我稍后在补充问题

    1.问题描述

    Tomcat 7下项目切换到Tomcat 8后,出现乱码。 无论Google还是百度,多数解决方法是server.xml设置URIEncoding=”UTF-8”,这种配置为了解决GET请求的中文乱码问题。 对于Tomcat 7下遇到乱码问题,这样配置是正确的;但是对”Tomcat 7正常,切换到Tomcat 8”乱码的情况无效。

    2. 解决方案[个人不喜欢]

    Tomcat8的server.xml配置Connector节点添加属性URIEncoding=”ISO-8859-1”。
    参考: http://tomcat.apache.org/migration-8.html

    3. 官方文档解释

    https://tomcat.apache.org/tomcat-7.0-doc/config/http.html

    URIEncoding
    This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

    https://tomcat.apache.org/tomcat-8.0-doc/config/http.html

    URIEncoding
    This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

    Tomcat7对URI默认编码是ISO-8859-1
    Tomcat8对URI默认编码是UTF-8

    4. 关于编码问题

    4.1 Tomcat7这个URI默认的编码带来很多问题,下面这个应该很常见:

    1. new String(value.getBytes("ISO-8859-1"), param);

    如果server.xml配置上URIEncoding=”UTF-8”就不需要了。 进而项目直接迁移到Tomcat8,不修改server.xml,或者再次加上URIEncoding=”UTF-8”也是不会有问题。

    4.2 Tomcat8是不是就是因为开发者服务端转码麻烦,URI默认的编码改为”UTF-8”。

    对于在Tomcat8开发项目,就简单很多,不需要上面的那段繁琐的代码。但是从Tomcat7迁移上来的项目,要么,去掉上面的代码,要么server.xml添加URIEncoding=”ISO-8859-1”,浪费Tomcat8一番美意。

    4.3 当然,通过判断参数值是否乱码,进行编码也是很不错的

    1. import java.util.regex.Matcher;
    2. import java.util.regex.Pattern;
    3. public class ChineseUtill {
    4. private static boolean isChinese(char c) {
    5. Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
    6. if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
    7. || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
    8. || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
    9. || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
    10. || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
    11. || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
    12. return true;
    13. }
    14. return false;
    15. }
    16. public static boolean isMessyCode(String strName) {
    17. Pattern p = Pattern.compile("\s*| *| *| *");
    18. Matcher m = p.matcher(strName);
    19. String after = m.replaceAll("");
    20. String temp = after.replaceAll("\p{P}", "");
    21. char[] ch = temp.trim().toCharArray();
    22. float chLength = 0 ;
    23. float count = 0;
    24. for (int i = 0; i < ch.length; i++) {
    25. char c = ch[i];
    26. if (!Character.isLetterOrDigit(c)) {
    27. if (!isChinese(c)) {
    28. count = count + 1;
    29. }
    30. chLength++;
    31. }
    32. }
    33. float result = count / chLength ;
    34. if (result > 0.4) {
    35. return true;
    36. } else {
    37. return false;
    38. }
    39. }
    40. public static String toChinese(String msg){
    41. if(isMessyCode(msg)){
    42. try {
    43. return new String(msg.getBytes("ISO8859-1"), "UTF-8");
    44. } catch (Exception e) {
    45. }
    46. }
    47. return msg ;
    48. }
    49. }




  • 相关阅读:
    [Docker][ansible-playbook]3 持续集成环境之分布式部署
    [Jenkins][GitHub]2 持续集成环境初探
    [Jenkins][centos]1 持续集成 之 配置VNC,部署Jenkins
    [AWS][GUI][VNC]rhel 7 安装GUI ,配置VNC
    [Git]checkout 指定版本
    [Golang][Mac]Go 语言学习资料记录
    App测试札记
    摘记:代码检查错误列表
    摘记:Web应用系统测试内容
    摘记:LoadRunner
  • 原文地址:https://www.cnblogs.com/pangxiansheng/p/5679822.html
Copyright © 2011-2022 走看看