程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java gbk轉utf-8

Java gbk轉utf-8

編輯:關於JAVA

Java gbk轉utf-8。本站提示廣大學習愛好者:(Java gbk轉utf-8)文章只能為提供參考,不一定能成為您想要的結果。以下是Java gbk轉utf-8正文


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了!!!

以上就是本文的全部內容,希望本文的內容對大家的學習或許任務能帶來一定的協助,同時也希望多多支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved