欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java gbk转utf-8

程序员文章站 2024-03-07 10:20:32
1、文件转码:使用脚本 gbk转utf-8的脚本文件: #!/bin/bash file_suffix="java xml html vm js" # fi...

1、文件转码:使用脚本

gbk转utf-8的脚本文件:

#!/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、文件转码后,本地环境改成utf-8环境,可能会有部分乱码文件,手动修复

3、含有中文js引用增加charset="gbk"

   如依赖:static.360buying.com、shop.jd.com

4、打包编译编码:替换成utf-8

5、xml设置格式:以前可能为gbk或gb2312,改成utf-8

6、web.xml转成utf-8,请求拦截器字符编码

   如使用spring配置

 <!--character encoding filter(字符集拦截转换) -->
 <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、代码gbk使用

  主要是代码里面写死gbk方式

  如string.getbytes("gbk")替换

8、jdurl配置编码

增加<property name="charsetname" value="utf-8"/>

避税分页中含有中文乱码

如:jdurl的编码设置:

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

大概是这8条

重点在后面:

这时你会发现,页面的get请求请求服务器时还是会出现乱码,别慌,那是因为tomcat的编码你还没有设置.

利用request.setcharacterencoding("utf-8");来设置tomcat接收请求的编码格式,只对post方式提交的数据有效,对get方式提交的数据无效!

要设置get的编码,可以修改server.xml文件中,相应的端口的connector的属性:uriencoding="utf-8",这样,get方式提交的数据才会被正确解码。

 <connector port="8080" protocol="http/1.1"
 connectiontimeout="20000"
 redirectport="8443" uriencoding="utf-8" />

这样就ok了!!!

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!