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)過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ú)疑問的亂碼了。
) 這時(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"),這句話熟悉么,這句話的意思是:用"utf-8"編碼對(duì)客戶端的請(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ú)疑問的亂碼了 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)記來(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)文章
解決SpringBoot返回結(jié)果如果為null或空值不顯示處理問題
這篇文章主要介紹了解決SpringBoot返回結(jié)果如果為null或空值不顯示處理問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java基礎(chǔ)之JDK1.8新特性lambda表達(dá)式詳解
函數(shù)式接口有且僅有一個(gè)抽象方法,但是可以有多個(gè)非抽象方法的接口,函數(shù)式接口可以被隱式轉(zhuǎn)換為lambda表達(dá)式,這篇文章主要介紹了Java基礎(chǔ)之lambda表達(dá)式(JDK1.8新特性),需要的朋友可以參考下2023-08-08Java實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)游戲(敵機(jī)下落篇)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)游戲,敵機(jī)下落篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05java 使用JDBC構(gòu)建簡(jiǎn)單的數(shù)據(jù)訪問層實(shí)例詳解
以下是如何使用JDBC構(gòu)建一個(gè)數(shù)據(jù)訪問層,包括數(shù)據(jù)轉(zhuǎn)換(將從數(shù)據(jù)庫(kù)中查詢的數(shù)據(jù)封裝到對(duì)應(yīng)的對(duì)象中……),數(shù)據(jù)庫(kù)的建立,以及如何連接到數(shù)據(jù)庫(kù),需要的朋友可以參考下2016-11-11Java中的Map集合根據(jù)key值排序的實(shí)現(xiàn)
本文主要介紹了Java中的Map集合如何根據(jù)key值排序,包含使用TreeMap和使用lambda表達(dá)式和Stream流兩種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03java 地心坐標(biāo)系(ECEF)和WGS-84坐標(biāo)系(WGS84)互轉(zhuǎn)的實(shí)現(xiàn)
這篇文章主要介紹了java 地心坐標(biāo)系(ECEF)和WGS-84坐標(biāo)系(WGS84)互轉(zhuǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09