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*|\t*|\r*|\n*");
  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. }





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM