Turn Java gbk utf 8

  • 2020-06-03 06:22:42
  • OfStack

1. File transcoding: script is used

gbk to utf-8 script file:


#!/bin/bash
FILE_SUFFIX="java xml html vm js"
# FILE_SUFFIX="vm"
file_names=""
for x in $FILE_SUFFIX
do
 file_names=`find . -name "*.$x" | xargs file -I | grep -v utf-8 | awk -F " |:" '{print $1}'`
 for file_name in $file_names
 do
 # echo $file_name
 iconv -f cp936 -t UTF-8 $file_name >$file_name".new" &&
 mv -f "$file_name.new" "$file_name"
 done
 echo "$x ok"
done 
find . -name "*.xml" | xargs sed -i "" "/<?xml/s/GBK/UTF-8/g"
find . -name "*.xml" | xargs sed -i "" "/<?xml/s/GB2312/UTF-8/g"
echo "xml head is ok!"
find . -name "pom.xml" | xargs sed -i "" "/<encoding>/s/GBK/UTF-8/g"
find . -name "pom.xml" | xargs sed -i "" "/<encoding>/s/GB2312/UTF-8/g"
find . -name "pom.xml" | xargs sed -i "" "/project.build.sourceEncoding/s/GBK/UTF-8/g"
find . -name "pom.xml" | xargs sed -i "" "/project.reporting.outputEncoding/s/GBK/UTF-8/g"
find . -name "pom.xml" | xargs sed -i "" "s/pop-vender-common-pageframe/pop-vender-common-pageframe-utf8/g"
echo "pom.xml is ok!" 
find . -name "*.properties" | xargs sed -i "" "/input.encoding/s/GBK/UTF-8/g"
find . -name "*.properties" | xargs sed -i "" "/output.encoding/s/GBK/UTF-8/g"
echo "velocity properties is OK!"
find . -name "strut*.xml" | xargs sed -i "" '/struts.i18n.encoding/s/GBK/UTF-8/g'
echo "struts xml is ok!"
find . -name "*.vm" | xargs sed -i "" "s/\/common\/js\/jdmsg\/jd-msg.js/\/common\/js\/jdmsg\/jd-msg-utf8.js/g"
find . -name "*.vm" | xargs sed -i "" "/\/ui.datepicker.js/s/<script t/<script charset=\"GBK\" t/g"
find . -name "*.vm" | xargs sed -i "" "/\/jquery-calendar.js/s/<script t/<script charset=\"GBK\" t/g"
echo "vm is ok"
echo "finished"
# echo $file_names

2. After the file transcoding, the local environment will be changed to utf-8 environment, and part of the messy files may be repaired manually

3. Adding charset="gbk" with Chinese js references

Such as dependence: static.360buying.com, shop.jd.com

4. Package compilation code: replace with ES24en-8

5. xml format: Previously it may be gbk or gb2312, change to ES30en-8

6, web. xml to UTF-8, request interceptor character encoding

For example, use the spring configuration


 <!--Character Encoding filter( Character set interception conversion ) -->
 <filter>
 <filter-name>charsetFilter</filter-name>
 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 <init-param>
 <param-name>encoding</param-name>
 <param-value>UTF-8</param-value>
 </init-param>
 <init-param>
 <param-name>forceEncoding</param-name>
 <param-value>true</param-value>
 </init-param>
 </filter>

7. Code GBK

The main code inside write dead GBK way

Such as string. getBytes (" GBK ")

8. jdurl configuration code

increase < property name="charsetName" value="utf-8"/ >

Tax avoidance page contains Chinese garbled code

For example, the coding setting of jdurl:


 <bean class="com.jd.pop.component.url.PopJdUrl">
 <property name="url" value="${pop-vender.login.address}"/>
 <property name="charsetName" value="utf-8"/>
 </bean>

It's about these eight

Focus on the following:

You will notice that the get page will still appear when requesting the server. Don't panic because you haven't set the code for tomcat yet.

Using request. setCharacterEncoding (" UTF - 8 "); To set the encoding format for Tomcat to receive requests, valid only for data submitted in POST mode, invalid for data submitted in GET mode!

To set the encoding of GET, you can modify the property of Connector for the corresponding port in the server.xml file: URIEncoding=" UTF-8 "so that the data submitted in GET will be decoded correctly.


 <Connector port="8080" protocol="HTTP/1.1"
 connectionTimeout="20000"
 redirectPort="8443" URIEncoding="UTF-8" />

ok!!


Related articles: