一、网页/表单/文件日志乱码
1、打开${CATALINA_HOME}/conf/server.xml,修改http端口的URIEncoding
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
2、修改tomcat启动参数,${CATALINA_HOME}/bin路径下面setEnv.bat,setClasspath.bat,catalina.bat
增加
set "CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding=UTF8"
二、控制台日志乱码
在设置上述之后,控制台中文会乱码。有两种解决方法:
1、设置tomcat的i18n
set "CATALINA_OPTS=%CATALINA_OPTS% -Duser.language=en"
2、修改tomcat源码 ${CATALINA_HOME}/bin路径下面的tomcat-juli.jar 里面的 org.apache.juli.ClassLoaderLogManager
//修改这个方法
public synchronized boolean addLogger(final Logger logger) {
.....
String handlers = getProperty(loggerName + ".handlers");
if (handlers != null) {
logger.setUseParentHandlers(false);
StringTokenizer tok = new StringTokenizer(handlers, ",");
while (tok.hasMoreTokens()) {
String handlerName = (tok.nextToken().trim());
Handler handler = null;
ClassLoader current = classLoader;
while (current != null) {
info = classLoaderLoggers.get(current);
if (info != null) {
handler = info.handlers.get(handlerName);
if (handler != null) {
break;
}
}
current = current.getParent();
}
if (handler != null) {
//Added by Allen 增加可以设置编码
//Date 2016 2016年10月25日 下午9:29:33
String encoding = getProperty(handlerName + ".encoding");
if (encoding != null) {
try {
handler.setEncoding(encoding);
} catch (SecurityException | UnsupportedEncodingException e) {}
}
logger.addHandler(handler);
}
}
}
.....
}
然后在${CATALINA_HOME}/conf/logging.properties里面增加
java.util.logging.ConsoleHandler.encoding = GBK
就可以了。
三、总结
现在UTF8字符集支持所有语言,所以优先使用UTF8。
JDK 里面的日志会获取系统用户的locale(请参考java.util.locale)来设置jdkLog的输出前缀和字符集。
zh/hans/TW/CN 等等都会设为GBK。
1、打开${CATALINA_HOME}/conf/server.xml,修改http端口的URIEncoding
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
2、修改tomcat启动参数,${CATALINA_HOME}/bin路径下面setEnv.bat,setClasspath.bat,catalina.bat
增加
set "CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding=UTF8"
二、控制台日志乱码
在设置上述之后,控制台中文会乱码。有两种解决方法:
1、设置tomcat的i18n
set "CATALINA_OPTS=%CATALINA_OPTS% -Duser.language=en"
2、修改tomcat源码 ${CATALINA_HOME}/bin路径下面的tomcat-juli.jar 里面的 org.apache.juli.ClassLoaderLogManager
//修改这个方法
public synchronized boolean addLogger(final Logger logger) {
.....
String handlers = getProperty(loggerName + ".handlers");
if (handlers != null) {
logger.setUseParentHandlers(false);
StringTokenizer tok = new StringTokenizer(handlers, ",");
while (tok.hasMoreTokens()) {
String handlerName = (tok.nextToken().trim());
Handler handler = null;
ClassLoader current = classLoader;
while (current != null) {
info = classLoaderLoggers.get(current);
if (info != null) {
handler = info.handlers.get(handlerName);
if (handler != null) {
break;
}
}
current = current.getParent();
}
if (handler != null) {
//Added by Allen 增加可以设置编码
//Date 2016 2016年10月25日 下午9:29:33
String encoding = getProperty(handlerName + ".encoding");
if (encoding != null) {
try {
handler.setEncoding(encoding);
} catch (SecurityException | UnsupportedEncodingException e) {}
}
logger.addHandler(handler);
}
}
}
.....
}
然后在${CATALINA_HOME}/conf/logging.properties里面增加
java.util.logging.ConsoleHandler.encoding = GBK
就可以了。
三、总结
现在UTF8字符集支持所有语言,所以优先使用UTF8。
JDK 里面的日志会获取系统用户的locale(请参考java.util.locale)来设置jdkLog的输出前缀和字符集。
zh/hans/TW/CN 等等都会设为GBK。
解决乱码的根本是 输入字符集和输出字符集一样。
tomcat7/jdk7 使用的tomcat-juli.jar我改过的发在下面图片里面。