Java涓枃缂栫爜灏忕粨

Java涓枃缂栫爜灏忕粨

1. 鍙湁 瀛楃鍒板瓧鑺� 鎴栬�� 瀛楄妭鍒板瓧绗� 鐨勮浆鎹㈡墠瀛樺湪缂栫爜杞爜;

2. Java String 閲囩敤 UTF-16 缂栫爜鏂瑰紡瀛樺偍鎵�鏈夊瓧绗︺��unicode浣撶郴閲囩敤鍞竴鐨勭爜鐐硅〃绀哄敮涓�鐨勫瓧绗︿俊鎭紝 鐮佺偣鐨勫瓨鍌ㄦ柟寮忔湁UFT-16銆乁TF-8 绛夌瓑銆�:  String represents a string in the UTF-16 format in which supplementary characters are represented bysurrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.  The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).

3. String鍙湁涓�绉嶆牸寮忥紝鍙涓篠tring鏄嫭绔嬩簬缂栫爜绯荤粺鐨勶紝閫氳繃  getBytes(String charsetName) 鍙疄鐜扮紪鐮佽浆鎹€��

4. String瀵硅薄鏄唴瀛樻暟鎹紝string涔嬮棿涓嶅瓨鍦ㄧ紪鐮佸彉鎹㈤棶棰樸�� 

5. 缂栫爜杞崲鍦烘櫙涓昏鍦� I/O 锛� I/O 鍖呮嫭纾佺洏 I/O 鍜岀綉缁� I/O锛氭枃浠惰緭鍏ヨ緭鍑恒�佸睆骞曘�佹暟鎹簱銆佹祻瑙堝櫒銆佹湇鍔″櫒銆�

6. 鍦ㄥ唴瀛樹腑鍊掕吘String鏁版嵁鏄紪鐮佹棤鍏崇殑锛屾瘮濡傚帇缂╃紪鐮併��

7. 缂栫爜璇尯: new String(str.getBytes("ISO-8859-1"), "GB18030") 杩欑鐢ㄦ硶鏄棤鎰忎箟鐨勶紝鐢氳嚦鏄敊璇殑銆傝繖绉嶇敤娉曟槸鐢℅B18030缂栫爜灏咺SO-8859-1缂栫爜鏍煎紡鐨勫瓧鑺傛暟鎹己鍒惰浆鎹㈡垚unicode鐮佺偣锛屼笉涔辩爜鏄繍姘旓紒

9. 鏁版嵁搴揓DBC鑳藉澶勭悊 鏁版嵁搴撴暟鎹� <=> String 鐨勬纭簰鎹€��

9. OutputStreamWriter 鍜� InputStreamWriter 搴旇鎸囧畾缂栫爜鏍煎紡锛岄伩鍏嶇▼搴忎緷璧栨搷浣滅郴缁熼粯璁ょ紪鐮併��

10. 鐢ㄦ埛浠庢祻瑙堝櫒绔彂璧蜂竴涓� HTTP 璇锋眰锛岄渶瑕佸瓨鍦ㄧ紪鐮佺殑鍦版柟鏄� URL銆丆ookie銆丳arameter銆傛湇鍔″櫒绔帴鍙楀埌 HTTP 璇锋眰鍚庤瑙f瀽 HTTP 鍗忚锛屽叾涓� URI銆丆ookie 鍜� POST 琛ㄥ崟鍙傛暟闇�瑕佽В鐮侊紝鏈嶅姟鍣ㄧ鍙兘杩橀渶瑕佽鍙栨暟鎹簱涓殑鏁版嵁锛屾湰鍦版垨缃戠粶涓叾瀹冨湴鏂圭殑鏂囨湰鏂囦欢锛岃繖浜涙暟鎹兘鍙兘瀛樺湪缂栫爜闂锛屽綋 Servlet 澶勭悊瀹屾墍鏈夎姹傜殑鏁版嵁鍚庯紝闇�瑕佸皢杩欎簺鏁版嵁鍐嶇紪鐮侀�氳繃 Socket 鍙戦�佸埌鐢ㄦ埛璇锋眰鐨勬祻瑙堝櫒閲岋紝鍐嶇粡杩囨祻瑙堝櫒瑙g爜鎴愪负鏂囨湰銆�

11. tomcat锛� URL 鐨� URI 閮ㄥ垎杩涜瑙g爜鐨勫瓧绗﹂泦鏄湪 connector 鐨� <Connector URIEncoding=”UTF-8”/>

12. QueryString(GET 鏌ヨ鍙傛暟) 鐨勮В鐮佸瓧绗﹂泦瑕佷箞鏄� Header 涓� ContentType 涓畾涔夌殑 Charset 瑕佷箞灏辨槸榛樿鐨� ISO-8859-1锛岃浣跨敤 ContentType 涓畾涔夌殑缂栫爜灏辫璁剧疆 connector 鐨� <Connector URIEncoding=”UTF-8” useBodyEncodingForURI=”true”/> 涓殑 useBodyEncodingForURI 璁剧疆涓� true銆�

13. 涓嶈鍦� Header 涓紶閫掗潪 ASCII 瀛楃锛屽鏋滀竴瀹氳浼犻�掔殑璇濓紝鎴戜滑鍙互鍏堝皢杩欎簺瀛楃鐢� org.apache.catalina.util.URLEncoder 缂栫爜鐒跺悗鍐嶆坊鍔犲埌 Header 涓紝杩欐牱鍦ㄦ祻瑙堝櫒鍒版湇鍔″櫒鐨勪紶閫掕繃绋嬩腑灏变笉浼氫涪澶变俊鎭簡锛屽鏋滄垜浠璁块棶杩欎簺椤规椂鍐嶆寜鐓х浉搴旂殑瀛楃闆嗚В鐮佸氨濂戒簡銆�

14. POST 琛ㄥ崟鐨勭紪瑙g爜: 閫氳繃 HTTP 鐨� BODY 浼犻�掑埌鏈嶅姟绔殑銆傚綋鎴戜滑鍦ㄩ〉闈笂鐐瑰嚮 submit 鎸夐挳鏃舵祻瑙堝櫒棣栧厛灏嗘牴鎹� ContentType 鐨� Charset 缂栫爜鏍煎紡瀵硅〃鍗曞~鐨勫弬鏁拌繘琛岀紪鐮佺劧鍚庢彁浜ゅ埌鏈嶅姟鍣ㄧ锛屽湪鏈嶅姟鍣ㄧ鍚屾牱涔熸槸鐢� ContentType 涓瓧绗﹂泦杩涜瑙g爜銆傛墍浠ラ�氳繃 POST 琛ㄥ崟鎻愪氦鐨勫弬鏁颁竴鑸笉浼氬嚭鐜伴棶棰橈紝鑰屼笖杩欎釜瀛楃闆嗙紪鐮佹槸鎴戜滑鑷繁璁剧疆鐨勶紝鍙互閫氳繃 request.setCharacterEncoding(charset) 鏉ヨ缃��

15. HTTP BODY 鐨勭紪瑙g爜: 褰撶敤鎴疯姹傜殑璧勬簮宸茬粡鎴愬姛鑾峰彇鍚庯紝杩欎簺鍐呭灏嗛�氳繃 Response 杩斿洖缁欏鎴风娴忚鍣紝杩欎釜杩囩▼鍏堣缁忚繃缂栫爜鍐嶅埌娴忚鍣ㄨ繘琛岃В鐮併�傝繖涓繃绋嬬殑缂栬В鐮佸瓧绗﹂泦鍙互閫氳繃 response.setCharacterEncoding 鏉ヨ缃紝瀹冨皢浼氳鐩� request.getCharacterEncoding 鐨勫�硷紝骞朵笖閫氳繃 Header 鐨� Content-Type 杩斿洖瀹㈡埛绔紝娴忚鍣ㄦ帴鍙楀埌杩斿洖鐨� socket 娴佹椂灏嗛�氳繃 Content-Type 鐨� charset 鏉ヨВ鐮侊紝濡傛灉杩斿洖鐨� HTTP Header 涓� Content-Type 娌℃湁璁剧疆 charset锛岄偅涔堟祻瑙堝櫒灏嗘牴鎹� Html 鐨� <meta HTTP-equiv="Content-Type" content="text/html; charset=GBK" /> 涓殑 charset 鏉ヨВ鐮併�傚鏋滀篃娌℃湁瀹氫箟鐨勮瘽锛岄偅涔堟祻瑙堝櫒灏嗕娇鐢ㄩ粯璁ょ殑缂栫爜鏉ヨВ鐮併��<%@ page contentType="text/html; charset= GBK" %>銆傝璁剧疆鍜宺esponse.setCharacterEncoding("GBK")绛夋晥銆�

绀轰緥浠g爜

/**
 * @author zhenjing
 * 
 * @date 2013-9-7
 */
public class cnCodeTest {

    public static void toHex(char[] b) {
        for (int i = 0; i < b.length; i++) {
            System.out.printf("%x " , (int)b[i]);
        }
        System.out.println();
    }

    public static void toHex(byte[] b) {
        for (int i = 0; i < b.length; i++) {
            System.out.printf("%x " , b[i]);
        }
        System.out.println();
    }
    
    public static void encode() {
        String name = "I am 涓枃缂栫爜";
        toHex(name.toCharArray());
        try {
            byte[] iso8859 = name.getBytes("ISO-8859-1");
            toHex(iso8859);
            byte[] gb2312 = name.getBytes("GB2312");
            toHex(gb2312);
            byte[] gbk = name.getBytes("GBK");
            toHex(gbk);
            byte[] utf16 = name.getBytes("UTF-16");
            toHex(utf16);
            byte[] utf8 = name.getBytes("UTF-8");
            toHex(utf8);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String cn = "涓枃缂栫爜";  // 杩欓噷瀛樺湪缂栫爜杞崲: 灏嗘枃浠跺瓨鍌ㄥ瓧鑺傝浆鎴恥nicode瀛樺叆String瀵硅薄鍐呭瓨. 閲囩敤鏂囦欢缂栫爜

        char[] charArray = cn.toCharArray();
        byte[] data = cn.getBytes();

        System.out.println("print char array : " + cn);
        toHex(cn.toCharArray());

        cn = "锟斤拷锟侥憋拷锟斤拷"; // 杩欓噷瀛樺湪缂栫爜杞崲: 灏嗘枃浠跺瓨鍌ㄥ瓧鑺傝浆鎴恥nicode瀛樺叆String瀵硅薄鍐呭瓨. 閲囩敤鏂囦欢缂栫爜銆� 
                        // 鏄剧ず涔辩爜鏄敱浜庢枃浠堕噰鐢ㄧ殑缂栫爜鏃犳硶瑙g爜鏂囦欢瀛樺偍瀛楄妭鏁版嵁銆傛晠瀛樺埌String鐨剈nicode涔熸槸涔辩爜鐨�
        charArray = cn.toCharArray();
        System.out.println("print char array: " + cn);
        toHex(cn.toCharArray());
        
        encode();
    }
}

 

鍙傝�冩枃绔�

娣卞叆鍒嗘瀽 Java 涓殑涓枃缂栫爜闂

JAVA瀛楃缂栫爜绯诲垪涓夛細Java搴旂敤涓殑缂栫爜闂

你可能感兴趣的:(Java涓枃缂栫爜灏忕粨)