上篇写到移动直连短信中使用了GBK编码,在本项目中运行时遭遇了乱码的袭击。
经过测试和分析后,将整个运行环境的编码统一成GBK编码即可解决这个问题。
本项目是运行在tomcat容器中的,以此对它进行配置即可,这里有两点需要进行配置:
1、修改server.xml,增加编码设置为GBK 1 <?xml version='1.0' encoding='utf-8'?>
2 <Server port="8085" shutdown="SHUTDOWN">
3
4 <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
5 <Listener className="org.apache.catalina.core.JasperListener" />
6 <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
7 <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
8 <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
9
10 <GlobalNamingResources>
11 <Resource name="UserDatabase" auth="Container"
12 type="org.apache.catalina.UserDatabase"
13 description="User database that can be updated and saved"
14 factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
15 pathname="conf/tomcat-users.xml" />
16 </GlobalNamingResources>
17
18 <Service name="Catalina">
19 <Connector port="8088" protocol="HTTP/1.1"
20 connectionTimeout="20000"
21 redirectPort="8443" URIEncoding='GBK'/>
22
23 <!-- Define an AJP 1.3 Connector on port 8009 -->
24 <Connector port="8809" protocol="AJP/1.3" redirectPort="8443" URIEncoding='GBK'/>
25
26 <Engine name="Catalina" defaultHost="localhost">
27 <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
28 resourceName="UserDatabase"/>
29
30 <Host name="localhost" appBase="webapps"
31 unpackWARs="true" autoDeploy="true"
32 xmlValidation="false" xmlNamespaceAware="false">
33
34
35 </Host>
36 </Engine>
37 </Service>
38 </Server>
2、修改setenv.sh启动参数,设置启动编码为GBK1 #JAVA_OPTS="-Xms128m -Xmx512m -XX:MaxPermSize=256m -Dfile.encoding=UTF-8 $JAVA_OPTS"
2 JAVA_OPTS="-Xms64m -Xmx256m -XX:MaxPermSize=128m -Dfile.encoding=GBK $JAVA_OPTS"
3
4 export JAVA_OPTS
技术文章收藏站点