感謝:https://blog.csdn.net/youngstar70/article/details/64117297
一、總結
在Java中,String的getBytes()方法是得到一個操作系統默認的編碼格式的字節數組。這個表示在不同情況下,返回的東西不一樣!
String.getBytes(String decode)方法會根據指定的decode編碼返回某字符串在該編碼下的byte數組表示,如:
byte[] b_gbk = "深".getBytes("GBK"); //b_gbk的長度為2 byte[] b_utf8 = "深".getBytes("UTF-8"); //b_utf8的長度為3 byte[] b_iso88591 = "深".getBytes("ISO8859-1");// b_iso88591的長度為1 byte[] b_unicode = "深".getBytes("unicode"); //b_unicode長度為4
將分別返回“深”這個漢字在GBK、UTF-8、ISO8859-1和unicode編碼下的byte數組表示,此時b_gbk的長度為2,b_utf8的長度為3,b_iso88591的長度為1,unicode為4。
而與getBytes相對的,可以通過new String(byte[], decode)的方式來還原這個“深”字時,這個new String(byte[], decode)實際是使用decode指定的編碼來將byte[]解析成字符串。
String s_gbk = new String(b_gbk,"GBK"); String s_utf8 = new String(b_utf8,"UTF-8"); String s_iso88591 = new String(b_iso88591,"ISO8859-1"); String s_unicode = new String(b_unicode, "unicode");
通過打印s_gbk、s_utf8、s_iso88591和unicode,會發現,s_gbk、s_utf8和unicode都是“深”,而只有s_iso88591是一個不認識的字符,為什么使用ISO8859-1編碼再組合之后,無法還原“深”字呢,其實原因很簡單,因為ISO8859-1編碼的編碼表中,根本就沒有包含漢字字符,當然也就無法通過"深".getBytes("ISO8859-1");來得到正確的“深”字在ISO8859-1中的編碼值了,所以再通過new String()來還原就無從談起了。
因此,通過String.getBytes(String decode)方法來得到byte[]時,一定要確定decode的編碼表中確實存在String表示的碼值,這樣得到的byte[]數組才能正確被還原。
有時候,為了讓中文字符適應某些特殊要求(如http header頭要求其內容必須為iso8859-1編碼),可能會通過將中文字符按照字節方式來編碼的情況,如
String s_iso88591 = new String("深".getBytes("UTF-8"),"ISO8859-1"),
這樣得到的s_iso8859-1字符串實際是三個在 ISO8859-1中的字符,在將這些字符傳遞到目的地后,目的地程序再通過相反的方式String s_utf8 = new String(s_iso88591.getBytes("ISO8859-1"),"UTF-8")來得到正確的中文漢字“深”。這樣就既保證了遵守協議規定、也支持中文。
同樣,在開發會檢查字符長度,以免數據庫字段的長度不夠而報錯,考慮到中英文的差異,肯定不能用String.length()方法判斷,而需采用String.getBytes().length;
而這方法將返回該操作系統默認的編碼格式的字節數組。如字符串“Hello!你好!”,在一個中文WindowsXP系統下,結果為12,而在英文的UNIX環境下,結果將為9。
因為該方法和平台(編碼)相關的。
在中文操作系統中,getBytes方法返回的是一個GBK或者GB2312的中文編碼的字節數組,其中中文字符,各占兩個字節。
而在英文平台中,一般的默認編碼是"ISO-8859-1",每個字符都只取一個字節(而不管是否非拉丁字符)。所以在這種情況下,應該給其傳入字符編碼字符串,即String.getBytes("GBK").length。
二、main方法測試
public static void main(String[] args) { String testData = "123abc數據"; //此次測試前提:參數包含中文 testU8(testData);//UTF_8-String:123abc數據 testIso(testData);//ISO_8859_1-String:123abc?? testChineseTrue(testData);//test3-String:123abc數據 testChineseFalse(testData);//test4-String:123abc?? } /** * 正常返回:UTF_8-String:123abc數據 * @param testdata */ public static void testU8(String testdata){ byte[] bytes = testdata.getBytes(CharsetUtil.UTF_8); String result = new String(bytes,CharsetUtil.UTF_8); System.out.println("UTF_8-String:"+result); } /** * 返回有亂碼:123abc?? * 原因:ISO8859-1編碼的編碼表中,根本就沒有包含漢字字符。 * @param testdata */ public static void testIso(String testdata){ byte[] bytes = testdata.getBytes(CharsetUtil.ISO_8859_1); String result = new String(bytes,CharsetUtil.ISO_8859_1); System.out.println("ISO_8859_1-String:"+result); } /** * 返回:123abc數據 * 通過String.getBytes(String decode)方法來得到byte[]時,一定要確定decode的編碼表中確實存在String表示的碼值。 * gbk/utf-8都可以 * @param testdata */ public static void testChineseTrue(String testdata){ Object message = null; message = new String(testdata.getBytes(CharsetUtil.GBK),CharsetUtil.ISO_8859_1); String returnData = message.toString(); //解析 byte[] xmlByte =returnData.getBytes(CharsetUtil.ISO_8859_1); String xml = ""; xml = new String(xmlByte,CharsetUtil.GBK); System.out.println("test3-String:"+xml); } /** * 當傳入的參數包含中文時,執行該方法出現亂碼。 返回:test4-String:123abc?? * 原因:通過String.getBytes(String decode)方法來得到byte[]時,一定要確定decode的編碼表中確實存在String表示的碼值。 * 而ISO8859-1編碼的編碼表中,根本就沒有包含漢字字符。 * @param testdata */ public static void testChineseFalse(String testdata){ Object message = null; message = new String(testdata.getBytes(CharsetUtil.ISO_8859_1),CharsetUtil.GBK); String returnData = message.toString(); //解析 byte[] xmlByte =returnData.getBytes(CharsetUtil.GBK); String xml = ""; xml = new String(xmlByte,CharsetUtil.ISO_8859_1); System.out.println("test4-String:"+xml); }
三、本地代碼解析:
//將接收到的message編碼后傳送給后台 public ChannelBuffer encode(Object message) throws TransportCodecException { try{ //message雖說是Object類型,看不到具體類型,但debug知道其數據格式為byte[],字節數組形式,可以強轉為byte[],然后再通過New String(byte[],charset)方法將byte[]轉換為String類型 //因為不確定接收到的message是什么編碼格式,這里會統一轉成gbk編碼。 message = new String((byte[])message,CharsetUtil.GBK); String gbkMessage = message.toString(); //當計算字段長度時,由於中英文的差異,肯定不能用String.length()方法判斷,而需采用String.getBytes().length; byte[] arrayOfByte = gbkMessage.getBytes(CharsetUtil.GBK); String requestByte = String.valueOf(arrayOfByte.length); String request = StringUtils.leftPad(requestByte,6,"0") + gbkMessage; LogConsole.info("xxx request message: " + request); return ChannelBuffers.copiedBuffer(request.getBytes(CharsetUtil.GBK)); // } catch(Exception e){ } return null; } //解析接收后台的報文,后台中文是用utf-8編碼的,然后整體用iso再封一次。 //所以我們接收到后,會先用iso解下,decode方法返回的數據格式是byte[],再通過new String(xmlByte,CharsetUtil.UTF_8)將其按照utf-8解析。 public Object decode(ChannelBuffer buffer) throws TransportCodecException { //ChannelBuffer該類型是本系統自己封裝,通過該方法將ChannelBuffer轉換為String類型,這時候的中文還是亂碼,之后會用utf-8解下。 String _message = buffer.toString(CharsetUtil.ISO_8859_1); if (_message.length() <= 0){ return null; } if (isDirect){ return buffer; } else{ if (exchangeLength == -1){ exchangeLength = fixedLength(_message,buffer);//后台返回報文格式為:6位的長度位+完整報文,該方法是處理長度位 } int readableLength = buffer.readableBytes(); if (hasRemaining(readableLength)){ clear(); LogConsole.info("xxx response message:" + _message); return _message.substring(fixedLength).getBytes(CharsetUtil.ISO_8859_1); } return null; } } //byte[]轉String xml = new String(xmlByte,CharsetUtil.UTF_8);