天天看點

jsp/tomcat/資料庫 中文後亂碼問題彙總

場景一:

SpringMvc架構下, jsp頁面中輸入中文傳到背景controller變成了亂碼, 前台列印:顯示正常, 在controller中打斷點後看到是亂碼

解決方案:

在web.xml中添加編碼過濾器 , 如下:

<!-- 編碼過濾器 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<!-- <async-supported>true</async-supported> -->
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
           
特别注意: web.xml中的攔截是有先後順序的一定要把編碼過濾器這段代碼放前面,否則極有可能不生效

場景二:

項目部署到tomcat運作後,列印的日志中出現一堆亂碼

jsp/tomcat/資料庫 中文後亂碼問題彙總

解決方案:

在 tomcat / conf 目錄下 logging.properties檔案中:

java.util.logging.ConsoleHandler.encoding = UTF-8
           

将 UTF-8 修改為 GBK,修改後的效果為:

java.util.logging.ConsoleHandler.encoding = GBK
           

儲存後,重新開機tomcat!

場景三:

資料庫存儲的中文是正常的,但是查詢後,傳至前台後資料變成了亂碼

解決方案:

1.首先排查你的資料源配置中是否指定了編碼格式

useUnicode=true characterEncoding=utf-8

注意下面配置中: &amp是&的轉義字元,在xml檔案中這樣配置是沒問題的,但是在**.properties就必須去掉才行

jdbc:mysql://127.0.0.1:3306/file_management?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull&amp;useSSL=false
           

2.在tomcat / conf 目錄下 server.xml檔案 修改如下:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
           
有同學說這個也要配置一下:
 <Connector port="7010" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
           

重新開機tomcat ,即可解決

場景4:

頁面亂碼,往往是在相應的jsp頁面或者html頁面設定相關的字元集即可解決。如:

<%@page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
           
JSP在部署後提供給使用者使用,會經過三個階段:

1 JSP生成java檔案:這個階段會使用pageEncoding所定義的編碼格式進行轉換

2 java檔案生成class檔案:這個階段由伺服器tomcat自動使用utf-8編碼把java檔案轉換成位元組碼class檔案

3 通過讀取class檔案展現給使用者:這個階段由tomcat伺服器擷取位元組碼内容,通過使用contentType所定義的編

碼格式展現給使用者。

大緻過程如下圖:

 

jsp/tomcat/資料庫 中文後亂碼問題彙總

另外html有時候也會亂碼:

meta中設定編碼即可

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 
</body>
</html>
           

場景5:

存入資料庫亂碼

1.檢視資料庫編碼格式:

jsp/tomcat/資料庫 中文後亂碼問題彙總

2.設定資料庫的編碼格式:

mysql> SET character_set_client='utf8';

mysql> SET character_set_connection='utf8'

mysql> SET character_set_results='utf8'
           

設定好編碼,下面便可以成功插入中文了

場景6:

doc視窗标題亂碼

場景:在運作jar包時 設定 編碼格式為utf-8,如下:

@ECHO OFF
title 智能運維系統-問題記錄
chcp 65001
IF "%SETENV%"=="" GOTO SETENV
GOTO END

:SETENV
SET JAVA_HOME=E:\Program Files\Java\jdk1.8.0_181
SET PATH=%JAVA_HOME%\bin;%PATH%;
:END
SET SETENV=SETENV
 
java -Dfile.encoding=utf-8 -jar -Xms512m -Xmx1024m D:\Web-spsolution-fileApp\yw\proj-0.0.1-SNAPSHOT.war  --server.port=8011 --spring.profiles.active=dev
           

然後檢視 doc視窗屬性後,也是設定的utf-8 如下圖,運作時卻出現了亂碼。

jsp/tomcat/資料庫 中文後亂碼問題彙總

解決方案:

修改系統資料庫:

1、快捷鍵win+R,打開運作視窗,輸入“regedit”,打開系統資料庫編輯器

2、修改系統資料庫的參數值,

修改系統資料庫路徑:HKEY_CURRENT_USER\Console\%"SystemRoot"%_system32_cmd.exe
健名:CodePage
修改前的值為:65001(十六進制),改為:65001(十進制);

注意:因為我之前設定過是以是65001
           

修改後,重新開機jar包,即可恢複正常

jsp/tomcat/資料庫 中文後亂碼問題彙總