java轉(zhuǎn)換字符串編碼格式的方法
java轉(zhuǎn)換字符串編碼格式 (解碼錯(cuò)誤,重新解碼)
字符集概念:規(guī)定了某個(gè)文字對(duì)應(yīng)的二進(jìn)制數(shù)字存放方式(編碼)和某串二進(jìn)制數(shù)值代表了哪個(gè)文字(解碼)的轉(zhuǎn)換關(guān)系。
我們?cè)谟?jì)算機(jī)屏幕上看到的是實(shí)體化的文字,而在計(jì)算機(jī)存儲(chǔ)介質(zhì)中存放的實(shí)際是二進(jìn)制的比特流。
亂碼場(chǎng)景(純屬瞎掰):
1) 前臺(tái)輸入utf-8編碼的一串漢字(string1)。 (頁(yè)面編碼為utf-8, 在內(nèi)存中會(huì)將這串漢字以u(píng)tf-8編碼為對(duì)應(yīng)的二進(jìn)制流存儲(chǔ))
2) 這串漢字(string1)的二進(jìn)制流在經(jīng)過(guò)http協(xié)議傳輸?shù)胶笈_(tái)時(shí),這段比特流會(huì)被以iso-8859-1編碼強(qiáng)行解碼為字符串(string2)。
(2.1 http默認(rèn)編碼格式為iso-8859-1)
(2.2 這個(gè)默認(rèn)編碼在什么時(shí)候起作用呢? 應(yīng)該是在到達(dá)tomcat之后, 到達(dá)servlet之前, tomcat對(duì)request請(qǐng)求強(qiáng)行使用iso-8859-1進(jìn)行了解碼)
(2.3 有什么辦法阻止tomcat對(duì)request請(qǐng)求強(qiáng)行iso-8859-1解碼呢?
apache-tomcat\conf\server.xml中添加URIEncoding="UTF-8"配置即可,還是來(lái)個(gè)圖吧)
3) 在后臺(tái)(servlet)接收字符串(string2)時(shí)毫無(wú)疑問(wèn)的亂碼了。
) 這時(shí)需要將接收到的字符串(string2)根據(jù)iso-8859-1編碼重新轉(zhuǎn)換為byte流。再將byte流根據(jù)utf-8編碼重新解碼為字符串(sting3)。
5) 這時(shí)的字符串(string3)和前臺(tái)的字符串(string1)是對(duì)應(yīng)同一個(gè)二進(jìn)制流,并且使用的是同一種編碼。也就不會(huì)亂碼了。
亂碼的另一種解決辦法:
request.setCharacterEncoding("UTF-8"),這句話(huà)熟悉么,這句話(huà)的意思是:用"utf-8"編碼對(duì)客戶(hù)端的請(qǐng)求進(jìn)行重新解碼。
在步驟2之后(或步驟3中)執(zhí)行,那么接收到的參數(shù)也不會(huì)亂碼啦。
一個(gè)小例子:
import java.io.UnsupportedEncodingException;
public class ConvertEncodingFormat {
/**
* 將一段錯(cuò)誤解碼的字符串重新解碼
*/
public static String convertEncodingFormat(String str, String formatFrom, String FormatTo) {
String result = null;
if (!(str == null || str.length() == 0)) {
try {
result = new String(str.getBytes(formatFrom), FormatTo);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
/**
* test
*/
public static void main(String[] args) {
// utf-8編碼
String str = "你好,少年!";
// UTF-8編碼的byte流強(qiáng)行用iso-8859-1解碼,毫無(wú)疑問(wèn)的亂碼了
String str1 = convertEncodingFormat(str, "UTF-8", "iso-8859-1");
System.out.println(str1);
// 將str1再轉(zhuǎn)化為byte流,重新用UTF-8解碼,亂碼問(wèn)題解決
String str2 = convertEncodingFormat(str1, "iso-8859-1", "UTF-8");
System.out.println(str2);
}
}
java字符串的各種編碼轉(zhuǎn)換
import java.io.UnsupportedEncodingException;
/**
* 轉(zhuǎn)換字符串的編碼
*/
public class ChangeCharset {
/** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁塊 */
public static final String US_ASCII = "US-ASCII";
/** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */
public static final String ISO_8859_1 = "ISO-8859-1";
/** 8 位 UCS 轉(zhuǎn)換格式 */
public static final String UTF_8 = "UTF-8";
/** 16 位 UCS 轉(zhuǎn)換格式,Big Endian(最低地址存放高位字節(jié))字節(jié)順序 */
public static final String UTF_16BE = "UTF-16BE";
/** 16 位 UCS 轉(zhuǎn)換格式,Little-endian(最高地址存放低位字節(jié))字節(jié)順序 */
public static final String UTF_16LE = "UTF-16LE";
/** 16 位 UCS 轉(zhuǎn)換格式,字節(jié)順序由可選的字節(jié)順序標(biāo)記來(lái)標(biāo)識(shí) */
public static final String UTF_16 = "UTF-16";
/** 中文超大字符集 */
public static final String GBK = "GBK";
/**
* 將字符編碼轉(zhuǎn)換成US-ASCII碼
*/
public String toASCII(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, US_ASCII);
}
/**
* 將字符編碼轉(zhuǎn)換成ISO-8859-1碼
*/
public String toISO_8859_1(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, ISO_8859_1);
}
/**
* 將字符編碼轉(zhuǎn)換成UTF-8碼
*/
public String toUTF_8(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_8);
}
/**
* 將字符編碼轉(zhuǎn)換成UTF-16BE碼
*/
public String toUTF_16BE(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_16BE);
}
/**
* 將字符編碼轉(zhuǎn)換成UTF-16LE碼
*/
public String toUTF_16LE(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_16LE);
}
/**
* 將字符編碼轉(zhuǎn)換成UTF-16碼
*/
public String toUTF_16(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_16);
}
/**
* 將字符編碼轉(zhuǎn)換成GBK碼
*/
public String toGBK(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, GBK);
}
/**
* 字符串編碼轉(zhuǎn)換的實(shí)現(xiàn)方法
* @param str 待轉(zhuǎn)換編碼的字符串
* @param newCharset 目標(biāo)編碼
* @return
* @throws UnsupportedEncodingException
*/
public String changeCharset(String str, String newCharset)
throws UnsupportedEncodingException {
if (str != null) {
//用默認(rèn)字符編碼解碼字符串。
byte[] bs = str.getBytes();
//用新的字符編碼生成字符串
return new String(bs, newCharset);
}
return null;
}
/**
* 字符串編碼轉(zhuǎn)換的實(shí)現(xiàn)方法
* @param str 待轉(zhuǎn)換編碼的字符串
* @param oldCharset 原編碼
* @param newCharset 目標(biāo)編碼
* @return
* @throws UnsupportedEncodingException
*/
public String changeCharset(String str, String oldCharset, String newCharset)
throws UnsupportedEncodingException {
if (str != null) {
//用舊的字符編碼解碼字符串。解碼可能會(huì)出現(xiàn)異常。
byte[] bs = str.getBytes(oldCharset);
//用新的字符編碼生成字符串
return new String(bs, newCharset);
}
return null;
}
public static void main(String[] args) throws UnsupportedEncodingException {
ChangeCharset test = new ChangeCharset();
String str = "This is a 中文的 String!";
System.out.println("str: " + str);
String gbk = test.toGBK(str);
System.out.println("轉(zhuǎn)換成GBK碼: " + gbk);
System.out.println();
String ascii = test.toASCII(str);
System.out.println("轉(zhuǎn)換成US-ASCII碼: " + ascii);
gbk = test.changeCharset(ascii,ChangeCharset.US_ASCII, ChangeCharset.GBK);
System.out.println("再把ASCII碼的字符串轉(zhuǎn)換成GBK碼: " + gbk);
System.out.println();
String iso88591 = test.toISO_8859_1(str);
System.out.println("轉(zhuǎn)換成ISO-8859-1碼: " + iso88591);
gbk = test.changeCharset(iso88591,ChangeCharset.ISO_8859_1, ChangeCharset.GBK);
System.out.println("再把ISO-8859-1碼的字符串轉(zhuǎn)換成GBK碼: " + gbk);
System.out.println();
String utf8 = test.toUTF_8(str);
System.out.println("轉(zhuǎn)換成UTF-8碼: " + utf8);
gbk = test.changeCharset(utf8,ChangeCharset.UTF_8, ChangeCharset.GBK);
System.out.println("再把UTF-8碼的字符串轉(zhuǎn)換成GBK碼: " + gbk);
System.out.println();
String utf16be = test.toUTF_16BE(str);
System.out.println("轉(zhuǎn)換成UTF-16BE碼:" + utf16be);
gbk = test.changeCharset(utf16be,ChangeCharset.UTF_16BE, ChangeCharset.GBK);
System.out.println("再把UTF-16BE碼的字符串轉(zhuǎn)換成GBK碼: " + gbk);
System.out.println();
String utf16le = test.toUTF_16LE(str);
System.out.println("轉(zhuǎn)換成UTF-16LE碼:" + utf16le);
gbk = test.changeCharset(utf16le,ChangeCharset.UTF_16LE, ChangeCharset.GBK);
System.out.println("再把UTF-16LE碼的字符串轉(zhuǎn)換成GBK碼: " + gbk);
System.out.println();
String utf16 = test.toUTF_16(str);
System.out.println("轉(zhuǎn)換成UTF-16碼:" + utf16);
gbk = test.changeCharset(utf16,ChangeCharset.UTF_16LE, ChangeCharset.GBK);
System.out.println("再把UTF-16碼的字符串轉(zhuǎn)換成GBK碼: " + gbk);
String s = new String("中文".getBytes("UTF-8"),"UTF-8");
System.out.println(s);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式
這篇文章主要介紹了淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式,使用設(shè)計(jì)模式編寫(xiě)代碼有利于團(tuán)隊(duì)協(xié)作時(shí)程序的維護(hù),需要的朋友可以參考下2016-01-01
Java使用JSONObject操作json實(shí)例解析
這篇文章主要介紹了Java使用JSONObject操作json,結(jié)合實(shí)例形式較為詳細(xì)的分析了Java使用JSONObject解析json數(shù)據(jù)相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
java中MultipartFile互轉(zhuǎn)File的方法
本文主要介紹了java中MultipartFile互轉(zhuǎn)File的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶(hù)端代碼圖文詳解
這篇文章主要介紹了IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶(hù)端代碼詳細(xì)流程,在這里小編使用的是idea2021.1最新開(kāi)發(fā)工具,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-05-05
淺談hibernate急迫加載問(wèn)題(多重外鍵關(guān)聯(lián))
這篇文章主要介紹了淺談hibernate急迫加載問(wèn)題(多重外鍵關(guān)聯(lián)),具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12

