字符的编码
package com.alibaba.china.gene.test;import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;/*** 模擬測(cè)試中文字符從瀏覽器到Web服務(wù)器Java端經(jīng)過的轉(zhuǎn)碼過程*/
public class UrlEncodeTest {public static void main(String[] args) {System.out.println("模擬測(cè)試中文字符從瀏覽器到Web服務(wù)器Java端經(jīng)過的轉(zhuǎn)碼過程");System.out.println("--------------------------------------------------");String str = "中文";String strGbk = "";String strUtf8 = "";try {strGbk = URLEncoder.encode(str, "gbk");strUtf8 = URLEncoder.encode(str, "utf8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}System.out.print("中文原字符串:");System.out.println(str);System.out.println("瀏覽器會(huì)做一次編碼,FireFox默認(rèn)gbk、IE默認(rèn)Utf-8:");System.out.print("中文對(duì)應(yīng)gbk編碼:");System.out.println(strGbk);System.out.print("中文對(duì)應(yīng)utf-8編碼:");System.out.println(strUtf8);System.out.println();System.out.println("在構(gòu)造Http請(qǐng)求頭時(shí),系統(tǒng)會(huì)按特定編碼轉(zhuǎn)成Byte流");System.out.print("中文原字符串轉(zhuǎn)成的Bytes流:");byte[] bytes = getInBytes(str);printBytes(bytes);System.out.print("中文對(duì)應(yīng)gbk編碼轉(zhuǎn)成的Bytes流:");byte[] bytesGbk = getInBytes(strGbk);printBytes(bytesGbk);System.out.print("中文對(duì)應(yīng)utf-8編碼轉(zhuǎn)成的Bytes流:");byte[] bytesUtf8 = getInBytes(strUtf8);printBytes(bytesUtf8);System.out.println();System.out.println("在發(fā)送Http請(qǐng)求給服務(wù)器時(shí),做網(wǎng)絡(luò)傳輸時(shí),系統(tǒng)都會(huì)轉(zhuǎn)成二進(jìn)制編碼");System.out.print("中文原字符串Bytes流對(duì)應(yīng)二進(jìn)制:");String[] binary = printAndGetInBinary(bytes);System.out.print("中文對(duì)應(yīng)gbk編碼Bytes流對(duì)應(yīng)二進(jìn)制:");String[] binaryGbk = printAndGetInBinary(bytesGbk);System.out.print("中文對(duì)應(yīng)utf-8編碼Bytes流對(duì)應(yīng)二進(jìn)制:");String[] binaryUtf8 = printAndGetInBinary(bytesUtf8);System.out.println();System.out.println("服務(wù)器接收到二進(jìn)制,系統(tǒng)都會(huì)轉(zhuǎn)成Bytes流");System.out.print("中文原字符串對(duì)應(yīng)二進(jìn)制還原得到Bytes流:");bytes = restoreBytes(binary);printBytes(bytes);System.out.print("中文對(duì)應(yīng)gbk編碼對(duì)應(yīng)二進(jìn)制還原得到Bytes流:");bytesGbk = restoreBytes(binaryGbk);printBytes(bytesGbk);System.out.print("中文對(duì)應(yīng)utf-8編碼對(duì)應(yīng)二進(jìn)制還原得到Bytes流:");bytesUtf8 = restoreBytes(binaryUtf8);printBytes(bytesUtf8);System.out.println();System.out.println("應(yīng)用服務(wù)器如Tomcat,默認(rèn)會(huì)默認(rèn)編碼還原成字符串編碼");str = new String(bytes);strGbk = new String(bytesGbk);strUtf8 = new String(bytesUtf8);System.out.print("中文原字符串Byte流還原得到的字符串編碼:");System.out.println(str);System.out.print("中文對(duì)應(yīng)gbk編碼Byte流還原得到的字符串編碼:");System.out.println(strGbk);System.out.print("中文對(duì)應(yīng)utf-8編碼Byte流還原得到的字符串編碼:");System.out.println(strUtf8);System.out.println();try {System.out.println("Java應(yīng)用,如Webx會(huì)按指定的編碼還原字符串");System.out.print("中文原字符串按gbk還原后:");System.out.println(URLDecoder.decode(str, "gbk"));System.out.println("這說明如果客戶端不進(jìn)行編碼直接發(fā)送中文給服務(wù)端,會(huì)造成信息丟失");System.out.print("中文對(duì)應(yīng)gbk編碼按gbk還原后:");System.out.println(URLDecoder.decode(strGbk, "gbk"));System.out.print("中文對(duì)應(yīng)utf-8編碼按utf-8還原后:");System.out.println(URLDecoder.decode(strUtf8, "utf-8"));System.out.println();System.out.println("Webx如果與瀏覽器使用的編碼不一致,還原出的字符串會(huì)是亂碼");System.out.print("中文對(duì)應(yīng)gbk編碼按utf-8還原后:");System.out.println(URLDecoder.decode(strGbk, "utf-8"));System.out.print("中文對(duì)應(yīng)utf-8編碼按gbk還原后:");System.out.println(URLDecoder.decode(strUtf8, "gbk"));System.out.println();} catch (UnsupportedEncodingException e) {e.printStackTrace();}try {System.out.println("數(shù)據(jù)庫中會(huì)轉(zhuǎn)成iso-8859-1編碼");str = "中文";System.out.print("中文字符串原文:");System.out.println(str);byte[] gbkBytes = str.getBytes("gbk");System.out.print("中文字符串對(duì)應(yīng)GBK的Byte流:");printBytes(gbkBytes);System.out.print("中文字符串對(duì)應(yīng)Byte流轉(zhuǎn)成的iso-8859-1格式字符串:");System.out.println(new String(gbkBytes, "iso-8859-1"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}System.out.println("--------------------------------------------------");}private static byte[] restoreBytes(String[] binary) {if (binary == null) {return new byte[0];}byte[] bytes = new byte[binary.length];for (int i = 0; i < binary.length; i++) {bytes[i] = (byte) Integer.parseInt(binary[i], 2);}return bytes;}private static String[] printAndGetInBinary(byte[] bytes) {if (bytes == null) {return new String[0];}String[] binaryStrs = new String[bytes.length];for (int i = 0; i < bytes.length; i++) {binaryStrs[i] = byte2bits(bytes[i]);}for (String string : binaryStrs) {System.out.print(string);}System.out.println();return binaryStrs;}public static String byte2bits(byte b) {int z = b;z |= 256;String str = Integer.toBinaryString(z);int len = str.length();return str.substring(len - 8, len);}private static void printBytes(byte[] bytes) {if (bytes == null) {return;}StringBuilder strBuilder = new StringBuilder();for (byte b : bytes) {strBuilder.append(b);}System.out.println(strBuilder.toString());}protected static byte[] getInBytes(String str) {if (str == null) {return null;}byte[] bytes = null;try {// 這里按iso-8859-1轉(zhuǎn)成Byte流bytes = str.getBytes("iso-8859-1");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return bytes;}}
轉(zhuǎn)載于:https://www.cnblogs.com/limingluzhu/p/4143094.html
總結(jié)
- 上一篇: 我的第三个jquery插件——promp
- 下一篇: mac iterm2快捷键