java轉(zhuǎn)換字符串編碼格式的方法
java轉(zhuǎn)換字符串編碼格式 (解碼錯誤,重新解碼)
字符集概念:規(guī)定了某個文字對應(yīng)的二進(jìn)制數(shù)字存放方式(編碼)和某串二進(jìn)制數(shù)值代表了哪個文字(解碼)的轉(zhuǎn)換關(guān)系。
我們在計算機(jī)屏幕上看到的是實體化的文字,而在計算機(jī)存儲介質(zhì)中存放的實際是二進(jìn)制的比特流。
亂碼場景(純屬瞎掰):
1) 前臺輸入utf-8編碼的一串漢字(string1)。 (頁面編碼為utf-8, 在內(nèi)存中會將這串漢字以utf-8編碼為對應(yīng)的二進(jìn)制流存儲)
2) 這串漢字(string1)的二進(jìn)制流在經(jīng)過http協(xié)議傳輸?shù)胶笈_時,這段比特流會被以iso-8859-1編碼強(qiáng)行解碼為字符串(string2)。
(2.1 http默認(rèn)編碼格式為iso-8859-1)
(2.2 這個默認(rèn)編碼在什么時候起作用呢? 應(yīng)該是在到達(dá)tomcat之后, 到達(dá)servlet之前, tomcat對request請求強(qiáng)行使用iso-8859-1進(jìn)行了解碼)
(2.3 有什么辦法阻止tomcat對request請求強(qiáng)行iso-8859-1解碼呢?
apache-tomcat\conf\server.xml中添加URIEncoding="UTF-8"配置即可,還是來個圖吧)
3) 在后臺(servlet)接收字符串(string2)時毫無疑問的亂碼了。
) 這時需要將接收到的字符串(string2)根據(jù)iso-8859-1編碼重新轉(zhuǎn)換為byte流。再將byte流根據(jù)utf-8編碼重新解碼為字符串(sting3)。
5) 這時的字符串(string3)和前臺的字符串(string1)是對應(yīng)同一個二進(jìn)制流,并且使用的是同一種編碼。也就不會亂碼了。
亂碼的另一種解決辦法:
request.setCharacterEncoding("UTF-8"),這句話熟悉么,這句話的意思是:用"utf-8"編碼對客戶端的請求進(jìn)行重新解碼。
在步驟2之后(或步驟3中)執(zhí)行,那么接收到的參數(shù)也不會亂碼啦。
一個小例子:
import java.io.UnsupportedEncodingException;
public class ConvertEncodingFormat {
/**
* 將一段錯誤解碼的字符串重新解碼
*/
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解碼,毫無疑問的亂碼了
String str1 = convertEncodingFormat(str, "UTF-8", "iso-8859-1");
System.out.println(str1);
// 將str1再轉(zhuǎn)化為byte流,重新用UTF-8解碼,亂碼問題解決
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)記來標(biāo)識 */
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)換的實現(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)換的實現(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) {
//用舊的字符編碼解碼字符串。解碼可能會出現(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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析Java設(shè)計模式編程中的單例模式和簡單工廠模式
這篇文章主要介紹了淺析Java設(shè)計模式編程中的單例模式和簡單工廠模式,使用設(shè)計模式編寫代碼有利于團(tuán)隊協(xié)作時程序的維護(hù),需要的朋友可以參考下2016-01-01
java中MultipartFile互轉(zhuǎn)File的方法
本文主要介紹了java中MultipartFile互轉(zhuǎn)File的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼圖文詳解
這篇文章主要介紹了IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼詳細(xì)流程,在這里小編使用的是idea2021.1最新開發(fā)工具,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-05-05
淺談hibernate急迫加載問題(多重外鍵關(guān)聯(lián))
這篇文章主要介紹了淺談hibernate急迫加載問題(多重外鍵關(guān)聯(lián)),具有一定借鑒價值,需要的朋友可以參考下。2017-12-12

