涓轰粈涔堣缂栫爜
涓嶇煡閬撳ぇ瀹舵湁娌℃湁鎯宠繃涓�涓棶棰橈紝閭e氨鏄负浠�涔堣缂栫爜锛熸垜浠兘涓嶈兘涓嶇紪鐮侊紵瑕佸洖绛旇繖涓棶棰樺繀椤昏鍥炲埌璁$畻鏈烘槸濡備綍琛ㄧず鎴戜滑浜虹被鑳藉鐞嗚В鐨勭鍙风殑锛岃繖浜涚鍙蜂篃灏辨槸鎴戜滑浜虹被浣跨敤鐨勮瑷�銆傜敱浜庝汉绫荤殑璇█鏈夊お澶氾紝鍥犺�岃〃绀鸿繖浜涜瑷�鐨勭鍙峰お澶氾紝鏃犳硶鐢ㄨ绠楁満涓竴涓熀鏈殑瀛樺偍鍗曞厓鈥斺�� byte 鏉ヨ〃绀猴紝鍥犺�屽繀椤昏缁忚繃鎷嗗垎鎴栦竴浜涚炕璇戝伐浣滐紝鎵嶈兘璁╄绠楁満鑳界悊瑙c�傛垜浠彲浠ユ妸璁$畻鏈鸿兘澶熺悊瑙g殑璇█鍋囧畾涓鸿嫳璇紝鍏跺畠璇█瑕佽兘澶熷湪璁$畻鏈轰腑浣跨敤蹇呴』缁忚繃涓�娆$炕璇戯紝鎶婂畠缈昏瘧鎴愯嫳璇�傝繖涓炕璇戠殑杩囩▼灏辨槸缂栫爜銆傛墍浠ュ彲浠ユ兂璞″彧瑕佷笉鏄鑻辫鐨勫浗瀹惰鑳藉浣跨敤璁$畻鏈哄氨蹇呴』瑕佺粡杩囩紪鐮併�傝繖鐪嬭捣鏉ユ湁浜涢湼閬擄紝浣嗘槸杩欏氨鏄幇鐘讹紝杩欎篃鍜屾垜浠浗瀹剁幇鍦ㄥ湪澶у姏鎺ㄥ箍姹夎涓�鏍凤紝甯屾湜鍏跺畠鍥藉閮戒細璇存眽璇紝浠ュ悗鍏跺畠鐨勮瑷�閮界炕璇戞垚姹夎锛屾垜浠彲浠ユ妸璁$畻鏈轰腑瀛樺偍淇℃伅鐨勬渶灏忓崟浣嶆敼鎴愭眽瀛楋紝杩欐牱鎴戜滑灏变笉瀛樺湪缂栫爜闂浜嗐��
鎵�浠ユ�荤殑鏉ヨ锛岀紪鐮佺殑鍘熷洜鍙互鎬荤粨涓猴細
璁$畻鏈轰腑瀛樺偍淇℃伅鐨勬渶灏忓崟鍏冩槸涓�涓瓧鑺傚嵆 8 涓� bit锛屾墍浠ヨ兘琛ㄧず鐨勫瓧绗﹁寖鍥存槸 0~255 涓�
浜虹被瑕佽〃绀虹殑绗﹀彿澶锛屾棤娉曠敤涓�涓瓧鑺傛潵瀹屽叏琛ㄧず
瑕佽В鍐宠繖涓煕鐩惧繀椤婚渶瑕佷竴涓柊鐨勬暟鎹粨鏋� char锛屼粠 char 鍒� byte 蹇呴』缂栫爜
濡備綍鈥滅炕璇戔��
鏄庣櫧浜嗗悇绉嶈瑷�闇�瑕佷氦娴侊紝缁忚繃缈昏瘧鏄繀瑕佺殑锛岄偅鍙堝浣曟潵缈昏瘧鍛紵璁$畻涓彁鎷变簡澶氱缈昏瘧鏂瑰紡锛屽父瑙佺殑鏈� ASCII銆両SO-8859-1銆丟B2312銆丟BK銆乁TF-8銆乁TF-16 绛夈�傚畠浠兘鍙互琚湅浣滀负瀛楀吀锛屽畠浠瀹氫簡杞寲鐨勮鍒欙紝鎸夌収杩欎釜瑙勫垯灏卞彲浠ヨ璁$畻鏈烘纭殑琛ㄧず鎴戜滑鐨勫瓧绗︺�傜洰鍓嶇殑缂栫爜鏍煎紡寰堝锛屼緥濡� GB2312銆丟BK銆乁TF-8銆乁TF-16 杩欏嚑绉嶆牸寮忛兘鍙互琛ㄧず涓�涓眽瀛楋紝閭f垜浠埌搴曢�夋嫨鍝缂栫爜鏍煎紡鏉ュ瓨鍌ㄦ眽瀛楀憿锛熻繖灏辫鑰冭檻鍒板叾瀹冨洜绱犱簡锛屾槸瀛樺偍绌洪棿閲嶈杩樻槸缂栫爜鐨勬晥鐜囬噸瑕併�傛牴鎹繖浜涘洜绱犳潵姝g‘閫夋嫨缂栫爜鏍煎紡锛�
涓嬮潰绠�瑕佷粙缁嶄竴涓嬭繖鍑犵缂栫爜鏍煎紡鈥斺��
ASCII 鐮�
瀛﹁繃璁$畻鏈虹殑浜洪兘鐭ラ亾 ASCII 鐮侊紝鎬诲叡鏈� 128 涓紝鐢ㄤ竴涓瓧鑺傜殑浣� 7 浣嶈〃绀猴紝0~31 鏄帶鍒跺瓧绗﹀鎹㈣鍥炶溅鍒犻櫎绛夛紱32~126 鏄墦鍗板瓧绗︼紝鍙互閫氳繃閿洏杈撳叆骞朵笖鑳藉鏄剧ず鍑烘潵銆�
ISO-8859-1
128 涓瓧绗︽樉鐒舵槸涓嶅鐢ㄧ殑锛屼簬鏄� ISO 缁勭粐鍦� ASCII 鐮佸熀纭�涓婂張鍒跺畾浜嗕竴浜涘垪鏍囧噯鐢ㄦ潵鎵╁睍 ASCII 缂栫爜锛屽畠浠槸 ISO-8859-1~ISO-8859-15锛屽叾涓� ISO-8859-1 娑电洊浜嗗ぇ澶氭暟瑗挎璇█瀛楃锛屾墍鏈夊簲鐢ㄧ殑鏈�骞挎硾銆侷SO-8859-1 浠嶇劧鏄崟瀛楄妭缂栫爜锛屽畠鎬诲叡鑳借〃绀� 256 涓瓧绗︺��
GB2312
瀹冪殑鍏ㄧО鏄�婁俊鎭氦鎹㈢敤姹夊瓧缂栫爜瀛楃闆� 鍩烘湰闆嗐�嬶紝瀹冩槸鍙屽瓧鑺傜紪鐮侊紝鎬荤殑缂栫爜鑼冨洿鏄� A1-F7锛屽叾涓粠 A1-A9 鏄鍙峰尯锛屾�诲叡鍖呭惈 682 涓鍙凤紝浠� B0-F7 鏄眽瀛楀尯锛屽寘鍚� 6763 涓眽瀛椼��
GBK
鍏ㄧО鍙�婃眽瀛楀唴鐮佹墿灞曡鑼冦�嬶紝鏄浗瀹舵妧鏈洃鐫e眬涓� windows95 鎵�鍒跺畾鐨勬柊鐨勬眽瀛楀唴鐮佽鑼冿紝瀹冪殑鍑虹幇鏄负浜嗘墿灞� GB2312锛屽姞鍏ユ洿澶氱殑姹夊瓧锛屽畠鐨勭紪鐮佽寖鍥存槸 8140~FEFE锛堝幓鎺� XX7F锛夋�诲叡鏈� 23940 涓爜浣嶏紝瀹冭兘琛ㄧず 21003 涓眽瀛楋紝瀹冪殑缂栫爜鏄拰 GB2312 鍏煎鐨勶紝涔熷氨鏄鐢� GB2312 缂栫爜鐨勬眽瀛楀彲浠ョ敤 GBK 鏉ヨВ鐮侊紝骞朵笖涓嶄細鏈変贡鐮併��
GB18030
鍏ㄧО鏄�婁俊鎭氦鎹㈢敤姹夊瓧缂栫爜瀛楃闆嗐�嬶紝鏄垜鍥界殑寮哄埗鏍囧噯锛屽畠鍙兘鏄崟瀛楄妭銆佸弻瀛楄妭鎴栬�呭洓瀛楄妭缂栫爜锛屽畠鐨勭紪鐮佷笌 GB2312 缂栫爜鍏煎锛岃繖涓櫧鐒舵槸鍥藉鏍囧噯锛屼絾鏄疄闄呭簲鐢ㄧ郴缁熶腑浣跨敤鐨勫苟涓嶅箍娉涖��
UTF-16
璇村埌 UTF 蹇呴』瑕佹彁鍒� Unicode锛圲niversal Code 缁熶竴鐮侊級锛孖SO 璇曞浘鎯冲垱寤轰竴涓叏鏂扮殑瓒呰瑷�瀛楀吀锛屼笘鐣屼笂鎵�鏈夌殑璇█閮藉彲浠ラ�氳繃杩欐湰瀛楀吀鏉ョ浉浜掔炕璇戙�傚彲鎯宠�岀煡杩欎釜瀛楀吀鏄涔堢殑澶嶆潅锛屽叧浜� Unicode 鐨勮缁嗚鑼冨彲浠ュ弬鑰冪浉搴旀枃妗c�俇nicode 鏄� Java 鍜� XML 鐨勫熀纭�锛屼笅闈㈣缁嗕粙缁� Unicode 鍦ㄨ绠楁満涓殑瀛樺偍褰㈠紡銆�
UTF-16 鍏蜂綋瀹氫箟浜� Unicode 瀛楃鍦ㄨ绠楁満涓瓨鍙栨柟娉曘�俇TF-16 鐢ㄤ袱涓瓧鑺傛潵琛ㄧず Unicode 杞寲鏍煎紡锛岃繖涓槸瀹氶暱鐨勮〃绀烘柟娉曪紝涓嶈浠�涔堝瓧绗﹂兘鍙互鐢ㄤ袱涓瓧鑺傝〃绀猴紝涓や釜瀛楄妭鏄� 16 涓� bit锛屾墍浠ュ彨 UTF-16銆俇TF-16 琛ㄧず瀛楃闈炲父鏂逛究锛屾瘡涓や釜瀛楄妭琛ㄧず涓�涓瓧绗︼紝杩欎釜鍦ㄥ瓧绗︿覆鎿嶄綔鏃跺氨澶уぇ绠�鍖栦簡鎿嶄綔锛岃繖涔熸槸
Java 浠� UTF-16 浣滀负鍐呭瓨鐨勫瓧绗﹀瓨鍌ㄦ牸寮�
鐨勪竴涓緢閲嶈鐨勫師鍥犮��
UTF-8
UTF-16 缁熶竴閲囩敤涓や釜瀛楄妭琛ㄧず涓�涓瓧绗︼紝铏界劧鍦ㄨ〃绀轰笂闈炲父绠�鍗曟柟渚匡紝浣嗘槸涔熸湁鍏剁己鐐癸紝鏈夊緢澶т竴閮ㄥ垎瀛楃鐢ㄤ竴涓瓧鑺傚氨 鍙互琛ㄧず鐨勭幇鍦ㄨ涓や釜瀛楄妭琛ㄧず锛屽瓨鍌ㄧ┖闂存斁澶т簡涓�鍊嶏紝鍦ㄧ幇鍦ㄧ殑缃戠粶甯﹀杩橀潪甯告湁闄愮殑浠婂ぉ锛岃繖鏍蜂細澧炲ぇ缃戠粶浼犺緭鐨勬祦閲忥紝鑰屼笖涔熸病蹇呰銆傝��
UTF-8 閲囩敤浜嗕竴绉嶅彉闀挎妧鏈紝姣忎釜缂栫爜鍖哄煙鏈変笉鍚岀殑瀛楃爜闀垮害銆備笉鍚岀被鍨嬬殑瀛楃鍙互鏄敱 1~6 涓瓧鑺傜粍鎴�
銆�
UTF-8 鏈変互涓嬬紪鐮佽鍒欙細
濡傛灉涓�涓瓧鑺傦紝鏈�楂樹綅锛堢 8 浣嶏級涓� 0锛岃〃绀鸿繖鏄竴涓� ASCII 瀛楃锛�00 - 7F锛夈�傚彲瑙侊紝鎵�鏈� ASCII 缂栫爜宸茬粡鏄� UTF-8 浜嗐��
濡傛灉涓�涓瓧鑺傦紝浠� 11 寮�澶达紝杩炵画鐨� 1 鐨勪釜鏁版殫绀鸿繖涓瓧绗︾殑瀛楄妭鏁帮紝渚嬪锛�110xxxxx 浠h〃瀹冩槸鍙屽瓧鑺� UTF-8 瀛楃鐨勯瀛楄妭銆�
濡傛灉涓�涓瓧鑺傦紝浠� 10 寮�濮嬶紝琛ㄧず瀹冧笉鏄瀛楄妭锛岄渶瑕佸悜鍓嶆煡鎵炬墠鑳藉緱鍒板綋鍓嶅瓧绗︾殑棣栧瓧鑺�
Java 涓渶瑕佺紪鐮佺殑鍦烘櫙
鍓嶉潰鎻忚堪浜嗗父瑙佺殑鍑犵缂栫爜鏍煎紡锛屼笅闈㈠皢浠嬬粛 Java 涓浣曞鐞嗗缂栫爜鐨勬敮鎸侊紝浠�涔堝満鍚堜腑闇�瑕佺紪鐮併��
I/O 鎿嶄綔涓瓨鍦ㄧ殑缂栫爜
鎴戜滑鐭ラ亾娑夊強鍒扮紪鐮佺殑鍦版柟涓�鑸兘鍦ㄥ瓧绗﹀埌瀛楄妭鎴栬�呭瓧鑺傚埌瀛楃鐨勮浆鎹笂锛岃�岄渶瑕佽繖绉嶈浆鎹㈢殑鍦烘櫙涓昏鏄湪 I/O 鐨勬椂鍊欙紝杩欎釜 I/O 鍖呮嫭纾佺洏 I/O 鍜岀綉缁� I/O锛屽叧浜庣綉缁� I/O 閮ㄥ垎鍦ㄥ悗闈㈠皢涓昏浠� Web 搴旂敤涓轰緥浠嬬粛銆備笅鍥炬槸 Java 涓鐞� I/O 闂鐨勬帴鍙o細
Reader 绫绘槸 Java 鐨� I/O 涓瀛楃鐨勭埗绫伙紝鑰� InputStream 绫绘槸璇诲瓧鑺傜殑鐖剁被锛孖nputStreamReader 绫诲氨鏄叧鑱斿瓧鑺傚埌瀛楃鐨勬ˉ姊侊紝瀹冭礋璐e湪 I/O 杩囩▼涓鐞嗚鍙栧瓧鑺傚埌瀛楃鐨勮浆鎹紝鑰屽叿浣撳瓧鑺傚埌瀛楃鐨勮В鐮佸疄鐜板畠鐢� StreamDecoder 鍘诲疄鐜帮紝鍦� StreamDecoder 瑙g爜杩囩▼涓繀椤荤敱鐢ㄦ埛鎸囧畾 Charset 缂栫爜鏍煎紡銆傚�煎緱娉ㄦ剰鐨勬槸濡傛灉浣犳病鏈夋寚瀹� Charset锛屽皢浣跨敤鏈湴鐜涓殑榛樿瀛楃闆嗭紝渚嬪鍦ㄤ腑鏂囩幆澧冧腑灏嗕娇鐢� GBK 缂栫爜銆�
鍐欑殑鎯呭喌涔熸槸绫讳技锛屽瓧绗︾殑鐖剁被鏄� Writer锛屽瓧鑺傜殑鐖剁被鏄� OutputStream锛岄�氳繃 OutputStreamWriter 杞崲瀛楃鍒板瓧鑺傘�傚涓嬪浘鎵�绀猴細
鍚屾牱 StreamEncoder 绫昏礋璐e皢瀛楃缂栫爜鎴愬瓧鑺傦紝缂栫爜鏍煎紡鍜岄粯璁ょ紪鐮佽鍒欎笌瑙g爜鏄竴鑷寸殑銆�
濡備笅闈竴娈典唬鐮侊紝瀹炵幇浜嗘枃浠剁殑璇诲啓鍔熻兘锛�
Java浠g爜
String聽file聽="c:/stream.txt";
String聽charset聽="UTF-8";
//聽鍐欏瓧绗︽崲杞垚瀛楄妭娴�
FileOutputStream聽outputStream聽=new聽FileOutputStream(file);
OutputStreamWriter聽writer聽=new聽OutputStreamWriter(
outputStream,聽charset);
try聽{
writer.write("杩欐槸瑕佷繚瀛樼殑涓枃瀛楃");
}finally聽{
writer.close();
}
//聽璇诲彇瀛楄妭杞崲鎴愬瓧绗�
FileInputStream聽inputStream聽=new聽FileInputStream(file);
InputStreamReader聽reader聽=new聽InputStreamReader(
inputStream,聽charset);
StringBuffer聽buffer聽=new聽StringBuffer();
char[]聽buf聽=newchar[64];
int聽count聽=0;
try聽{
while聽((count聽=聽reader.read(buf))聽!=聽-1)聽{
buffer.append(buffer,0,聽count);
}
}finally聽{
reader.close();
}
鍦ㄦ垜浠殑搴旂敤绋嬪簭涓秹鍙婂埌 I/O 鎿嶄綔鏃跺彧瑕佹敞鎰忔寚瀹氱粺涓�鐨勭紪瑙g爜 Charset 瀛楃闆嗭紝涓�鑸笉浼氬嚭鐜颁贡鐮侀棶棰橈紝鏈変簺搴旂敤绋嬪簭濡傛灉涓嶆敞鎰忔寚瀹氬瓧绗︾紪鐮侊紝涓枃鐜涓彇鎿嶄綔绯荤粺榛樿缂栫爜锛屽鏋滅紪瑙g爜閮藉湪涓枃鐜涓紝閫氬父涔熸病闂锛屼絾鏄繕鏄己鐑堢殑涓嶅缓璁娇鐢ㄦ搷浣滅郴缁熺殑榛樿缂栫爜锛屽洜涓鸿繖鏍凤紝浣犵殑搴旂敤绋嬪簭鐨勭紪鐮佹牸寮忓氨鍜岃繍琛岀幆澧冪粦瀹氳捣鏉ヤ簡锛屽湪璺ㄧ幆澧冧笅寰堝彲鑳藉嚭鐜颁贡鐮侀棶棰樸��
鍐呭瓨涓搷浣滀腑鐨勭紪鐮�
鍦� Java 寮�鍙戜腑闄や簡 I/O 娑夊強鍒扮紪鐮佸锛屾渶甯哥敤鐨勫簲璇ュ氨鏄湪鍐呭瓨涓繘琛屽瓧绗﹀埌瀛楄妭鐨勬暟鎹被鍨嬬殑杞崲锛孞ava 涓敤 String 琛ㄧず瀛楃涓诧紝鎵�浠� String 绫诲氨鎻愪緵杞崲鍒板瓧鑺傜殑鏂规硶锛屼篃鏀寔灏嗗瓧鑺傝浆鎹负瀛楃涓茬殑鏋勯�犲嚱鏁般�傚涓嬩唬鐮佺ず渚嬶細
Java浠g爜
String聽s聽="杩欐槸涓�娈典腑鏂囧瓧绗︿覆";
byte[]聽b聽=聽s.getBytes("UTF-8");
String聽n聽=new聽String(b,"UTF-8");
鍙﹀涓�涓槸宸茬粡琚搴熷純鐨� ByteToCharConverter 鍜� CharToByteConverter 绫伙紝瀹冧滑鍒嗗埆鎻愪緵浜� convertAll 鏂规硶鍙互瀹炵幇 byte[] 鍜� char[] 鐨勪簰杞�傚涓嬩唬鐮佹墍绀猴細
Java浠g爜
ByteToCharConverter聽charConverter聽=聽ByteToCharConverter.getConverter("UTF-8");
char聽c[]聽=聽charConverter.convertAll(byteArray);
CharToByteConverter聽byteConverter聽=聽CharToByteConverter.getConverter("UTF-8");
byte[]聽b聽=聽byteConverter.convertAll(c);
杩欎袱涓被宸茬粡琚� Charset 绫诲彇浠o紝Charset 鎻愪緵 encode 涓� decode 鍒嗗埆瀵瑰簲 char[] 鍒� byte[] 鐨勭紪鐮佸拰 byte[] 鍒� char[] 鐨勮В鐮併�傚涓嬩唬鐮佹墍绀猴細
Java浠g爜
Charset聽charset聽=聽Charset.forName("UTF-8");
ByteBuffer聽byteBuffer聽=聽charset.encode(string);
CharBuffer聽charBuffer聽=聽charset.decode(byteBuffer);
缂栫爜涓庤В鐮侀兘鍦ㄤ竴涓被涓畬鎴愶紝閫氳繃 forName 璁剧疆缂栬В鐮佸瓧绗﹂泦锛岃繖鏍锋洿瀹规槗缁熶竴缂栫爜鏍煎紡锛屾瘮 ByteToCharConverter 鍜� CharToByteConverter 绫绘洿鏂逛究銆�
Java 涓繕鏈変竴涓� ByteBuffer 绫伙紝瀹冩彁渚涗竴绉� char 鍜� byte 涔嬮棿鐨勮蒋杞崲锛屽畠浠箣闂磋浆鎹笉闇�瑕佺紪鐮佷笌瑙g爜锛屽彧鏄妸涓�涓� 16bit 鐨� char 鏍煎紡锛屾媶鍒嗘垚涓� 2 涓� 8bit 鐨� byte 琛ㄧず锛屽畠浠殑瀹為檯鍊煎苟娌℃湁琚慨鏀癸紝浠呬粎鏄暟鎹殑绫诲瀷鍋氫簡杞崲銆傚涓嬩唬鐮佹墍浠ワ細
Java浠g爜
ByteBuffer聽heapByteBuffer聽=聽ByteBuffer.allocate(1024);
ByteBuffer聽byteBuffer聽=聽heapByteBuffer.putChar(c);
浠ヤ笂杩欎簺鎻愪緵瀛楃鍜屽瓧鑺備箣闂寸殑鐩镐簰杞崲鍙鎴戜滑璁剧疆缂栬В鐮佹牸寮忕粺涓�涓�鑸兘涓嶄細鍑虹幇闂銆�
Java 涓浣曠紪瑙g爜
鍓嶉潰浠嬬粛浜嗗嚑绉嶅父瑙佺殑缂栫爜鏍煎紡锛岃繖閲屽皢浠ュ疄闄呬緥瀛愪粙缁� Java 涓浣曞疄鐜扮紪鐮佸強瑙g爜锛屼笅闈㈡垜浠互鈥淚 am 鍚涘北鈥濊繖涓瓧绗︿覆涓轰緥浠嬬粛 Java 涓浣曟妸瀹冧互 ISO-8859-1銆丟B2312銆丟BK銆乁TF-16銆乁TF-8 缂栫爜鏍煎紡杩涜缂栫爜鐨勩��
Java浠g爜
publicstaticvoid聽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聽(UnsupportedEncodingException聽e)聽{
e.printStackTrace();
}
}
鎴戜滑鎶� name 瀛楃涓叉寜鐓у墠闈㈣鐨勫嚑绉嶇紪鐮佹牸寮忚繘琛岀紪鐮佽浆鍖栨垚 byte 鏁扮粍锛岀劧鍚庝互 16 杩涘埗杈撳嚭锛屾垜浠厛鐪嬩竴涓� Java 鏄浣曡繘琛岀紪鐮佺殑銆�
涓嬮潰鏄� Java 涓紪鐮侀渶瑕佺敤鍒扮殑绫诲浘
鍥� 1. Java 缂栫爜绫诲浘
棣栧厛鏍规嵁鎸囧畾鐨� charsetName 閫氳繃 Charset.forName(charsetName) 璁剧疆 Charset 绫伙紝鐒跺悗鏍规嵁 Charset 鍒涘缓 CharsetEncoder 瀵硅薄锛屽啀璋冪敤 CharsetEncoder.encode 瀵瑰瓧绗︿覆杩涜缂栫爜锛屼笉鍚岀殑缂栫爜绫诲瀷閮戒細瀵瑰簲鍒颁竴涓被涓紝瀹為檯鐨勭紪鐮佽繃绋嬫槸鍦ㄨ繖浜涚被涓畬鎴愮殑銆備笅闈㈡槸 String. getBytes(charsetName) 缂栫爜杩囩▼鐨勬椂搴忓浘
鍥� 2.Java 缂栫爜鏃跺簭鍥�
浠庝笂鍥惧彲浠ョ湅鍑烘牴鎹� charsetName 鎵惧埌 Charset 绫伙紝鐒跺悗鏍规嵁杩欎釜瀛楃闆嗙紪鐮佺敓鎴� CharsetEncoder锛岃繖涓被鏄墍鏈夊瓧绗︾紪鐮佺殑鐖剁被锛岄拡瀵逛笉鍚岀殑瀛楃缂栫爜闆嗗湪鍏跺瓙绫讳腑瀹氫箟浜嗗浣曞疄鐜扮紪鐮侊紝鏈変簡 CharsetEncoder 瀵硅薄鍚庡氨鍙互璋冪敤 encode 鏂规硶鍘诲疄鐜扮紪鐮佷簡銆傝繖涓槸 String.getBytes 缂栫爜鏂规硶锛屽叾瀹冪殑濡� StreamEncoder 涓篃鏄被浼肩殑鏂瑰紡銆備笅闈㈢湅鐪嬩笉鍚岀殑瀛楃闆嗘槸濡備綍灏嗗墠闈㈢殑瀛楃涓茬紪鐮佹垚 byte 鏁扮粍鐨勶紵
濡傚瓧绗︿覆鈥淚 am 鍚涘北鈥濈殑 char 鏁扮粍涓� 49 20 61 6d 20 541b 5c71锛屼笅闈㈡妸瀹冩寜鐓т笉鍚岀殑缂栫爜鏍煎紡杞寲鎴愮浉搴旂殑瀛楄妭銆�
鎸夌収 ISO-8859-1 缂栫爜
瀛楃涓测�淚 am 鍚涘北鈥濈敤 ISO-8859-1 缂栫爜锛屼笅闈㈡槸缂栫爜缁撴灉锛�
浠庝笂鍥剧湅鍑� 7 涓� char 瀛楃缁忚繃 ISO-8859-1 缂栫爜杞彉鎴� 7 涓� byte 鏁扮粍锛孖SO-8859-1 鏄崟瀛楄妭缂栫爜锛屼腑鏂団�滃悰灞扁�濊杞寲鎴愬�兼槸 3f 鐨� byte銆�3f 涔熷氨鏄�滐紵鈥濆瓧绗︼紝鎵�浠ョ粡甯镐細鍑虹幇涓枃鍙樻垚鈥滐紵鈥濆緢鍙兘灏辨槸閿欒鐨勪娇鐢ㄤ簡 ISO-8859-1 杩欎釜缂栫爜瀵艰嚧鐨勩�備腑鏂囧瓧绗︾粡杩� ISO-8859-1 缂栫爜浼氫涪澶变俊鎭紝閫氬父鎴戜滑绉颁箣涓衡�滈粦娲炩�濓紝瀹冧細鎶婁笉璁よ瘑鐨勫瓧绗﹀惛鏀舵帀銆傜敱浜庣幇鍦ㄥぇ閮ㄥ垎鍩虹鐨� Java 妗嗘灦鎴栫郴缁熼粯璁ょ殑瀛楃闆嗙紪鐮侀兘鏄� ISO-8859-1锛屾墍浠ュ緢瀹规槗鍑虹幇涔辩爜闂锛屽悗闈㈠皢浼氬垎鏋愪笉鍚岀殑涔辩爜褰㈠紡鏄�庝箞鍑虹幇鐨勩��
鎸夌収 GB2312 缂栫爜
瀛楃涓测�淚 am 鍚涘北鈥濈敤 GB2312 缂栫爜锛屼笅闈㈡槸缂栫爜缁撴灉锛�
GB2312 瀵瑰簲鐨� Charset 鏄� sun.nio.cs.ext. EUC_CN 鑰屽搴旂殑 CharsetDecoder 缂栫爜绫绘槸 sun.nio.cs.ext. DoubleByte锛孏B2312 瀛楃闆嗘湁涓�涓� char 鍒� byte 鐨勭爜琛紝涓嶅悓鐨勫瓧绗︾紪鐮佸氨鏄煡杩欎釜鐮佽〃鎵惧埌涓庢瘡涓瓧绗︾殑瀵瑰簲鐨勫瓧鑺傦紝鐒跺悗鎷艰鎴� byte 鏁扮粍銆傛煡琛ㄧ殑瑙勫垯濡備笅锛�
Java浠g爜
c2b[c2bIndex[char聽>>8]聽+聽(char聽&0xff)]
濡傛灉鏌ュ埌鐨勭爜浣嶅�煎ぇ浜� oxff 鍒欐槸鍙屽瓧鑺傦紝鍚﹀垯鏄崟瀛楄妭銆傚弻瀛楄妭楂� 8 浣嶄綔涓虹涓�涓瓧鑺傦紝浣� 8 浣嶄綔涓虹浜屼釜瀛楄妭锛屽涓嬩唬鐮佹墍绀猴細
Java浠g爜
if聽(bb聽>0xff)聽{//聽DoubleByte
if聽(dl聽-聽dp聽<2)
return聽CoderResult.OVERFLOW;
da[dp++]聽=聽(byte)聽(bb聽>>8);
da[dp++]聽=聽(byte)聽bb;
}else聽{//聽SingleByte
if聽(dl聽-聽dp聽<1)
return聽CoderResult.OVERFLOW;
da[dp++]聽=聽(byte)聽bb;
}
浠庝笂鍥惧彲浠ョ湅鍑哄墠 5 涓瓧绗︾粡杩囩紪鐮佸悗浠嶇劧鏄� 5 涓瓧鑺傦紝鑰屾眽瀛楄缂栫爜鎴愬弻瀛楄妭锛屽湪绗竴鑺備腑浠嬬粛鍒� GB2312 鍙敮鎸� 6763 涓眽瀛楋紝鎵�浠ュ苟涓嶆槸鎵�鏈夋眽瀛楅兘鑳藉鐢� GB2312 缂栫爜銆�
鎸夌収 GBK 缂栫爜
瀛楃涓测�淚 am 鍚涘北鈥濈敤 GBK 缂栫爜锛屼笅闈㈡槸缂栫爜缁撴灉锛�
浣犲彲鑳藉凡缁忓彂鐜颁笂鍥句笌 GB2312 缂栫爜鐨勭粨鏋滄槸涓�鏍风殑锛屾病閿� GBK 涓� GB2312 缂栫爜缁撴灉鏄竴鏍风殑锛岀敱姝ゅ彲浠ュ緱鍑� GBK 缂栫爜鏄吋瀹� GB2312 缂栫爜鐨勶紝瀹冧滑鐨勭紪鐮佺畻娉曚篃鏄竴鏍风殑銆備笉鍚岀殑鏄畠浠殑鐮佽〃闀垮害涓嶄竴鏍凤紝GBK 鍖呭惈鐨勬眽瀛楀瓧绗︽洿澶氥�傛墍浠ュ彧瑕佹槸缁忚繃 GB2312 缂栫爜鐨勬眽瀛楅兘鍙互鐢� GBK 杩涜瑙g爜锛屽弽杩囨潵鍒欎笉鐒躲��
鎸夌収 UTF-16 缂栫爜
瀛楃涓测�淚 am 鍚涘北鈥濈敤 UTF-16 缂栫爜锛屼笅闈㈡槸缂栫爜缁撴灉锛�
鐢� UTF-16 缂栫爜灏� char 鏁扮粍鏀惧ぇ浜嗕竴鍊嶏紝鍗曞瓧鑺傝寖鍥村唴鐨勫瓧绗︼紝鍦ㄩ珮浣嶈ˉ 0 鍙樻垚涓や釜瀛楄妭锛屼腑鏂囧瓧绗︿篃鍙樻垚涓や釜瀛楄妭銆備粠 UTF-16 缂栫爜瑙勫垯鏉ョ湅锛屼粎浠呭皢瀛楃鐨勯珮浣嶅拰鍦颁綅杩涜鎷嗗垎鍙樻垚涓や釜瀛楄妭銆傜壒鐐规槸缂栫爜鏁堢巼闈炲父楂橈紝瑙勫垯寰堢畝鍗曪紝鐢变簬涓嶅悓澶勭悊鍣ㄥ 2 瀛楄妭澶勭悊鏂瑰紡涓嶅悓锛孊ig-endian锛堥珮浣嶅瓧鑺傚湪鍓嶏紝浣庝綅瀛楄妭鍦ㄥ悗锛夋垨 Little-endian锛堜綆浣嶅瓧鑺傚湪鍓嶏紝楂樹綅瀛楄妭鍦ㄥ悗锛夌紪鐮侊紝鎵�浠ュ湪瀵逛竴涓插瓧绗︿覆杩涜缂栫爜鏄渶瑕佹寚鏄庡埌搴曟槸 Big-endian 杩樻槸 Little-endian锛屾墍浠ュ墠闈㈡湁涓や釜瀛楄妭鐢ㄦ潵淇濆瓨 BYTE_ORDER_MARK 鍊硷紝UTF-16 鏄敤瀹氶暱 16 浣嶏紙2 瀛楄妭锛夋潵琛ㄧず鐨� UCS-2 鎴� Unicode 杞崲鏍煎紡锛岄�氳繃浠g悊瀵规潵璁块棶 BMP 涔嬪鐨勫瓧绗︾紪鐮併��
鎸夌収 UTF-8 缂栫爜
瀛楃涓测�淚 am 鍚涘北鈥濈敤 UTF-8 缂栫爜锛屼笅闈㈡槸缂栫爜缁撴灉锛�
UTF-16 铏界劧缂栫爜鏁堢巼寰堥珮锛屼絾鏄鍗曞瓧鑺傝寖鍥村唴瀛楃涔熸斁澶т簡涓�鍊嶏紝杩欐棤褰篃娴垂浜嗗瓨鍌ㄧ┖闂达紝鍙﹀ UTF-16 閲囩敤椤哄簭缂栫爜锛屼笉鑳藉鍗曚釜瀛楃鐨勭紪鐮佸�艰繘琛屾牎楠岋紝濡傛灉涓棿鐨勪竴涓瓧绗︾爜鍊兼崯鍧忥紝鍚庨潰鐨勬墍鏈夌爜鍊奸兘灏嗗彈褰卞搷銆傝�� UTF-8 杩欎簺闂閮戒笉瀛樺湪锛孶TF-8 瀵瑰崟瀛楄妭鑼冨洿鍐呭瓧绗︿粛鐒剁敤涓�涓瓧鑺傝〃绀猴紝瀵规眽瀛楅噰鐢ㄤ笁涓瓧鑺傝〃绀恒�傚畠鐨勭紪鐮佽鍒欏涓嬶細
Java浠g爜
private聽CoderResult聽encodeArrayLoop(CharBuffer聽src,
ByteBuffer聽dst){
char[]聽sa聽=聽src.array();
int聽sp聽=聽src.arrayOffset()聽+聽src.position();
int聽sl聽=聽src.arrayOffset()聽+聽src.limit();
byte[]聽da聽=聽dst.array();
int聽dp聽=聽dst.arrayOffset()聽+聽dst.position();
int聽dl聽=聽dst.arrayOffset()聽+聽dst.limit();
int聽dlASCII聽=聽dp聽+聽Math.min(sl聽-聽sp,聽dl聽-聽dp);
//聽ASCII聽only聽loop
while聽(dp聽<聽dlASCII聽&&聽sa[sp]聽<'\u0080')
da[dp++]聽=聽(byte)聽sa[sp++];
while聽(sp聽<聽sl)聽{
char聽c聽=聽sa[sp];
if聽(c聽<0x80)聽{
//聽Have聽at聽most聽seven聽bits
if聽(dp聽>=聽dl)
return聽overflow(src,聽sp,聽dst,聽dp);
da[dp++]聽=聽(byte)c;
}elseif聽(c聽<0x800)聽{
//聽2聽bytes,聽11聽bits
if聽(dl聽-聽dp聽<2)
return聽overflow(src,聽sp,聽dst,聽dp);
da[dp++]聽=聽(byte)(0xc0|聽(c聽>>6));
da[dp++]聽=聽(byte)(0x80|聽(c聽&0x3f));
}elseif聽(Character.isSurrogate(c))聽{
//聽Have聽a聽surrogate聽pair
if聽(sgp聽==null)
sgp聽=new聽Surrogate.Parser();
int聽uc聽=聽sgp.parse(c,聽sa,聽sp,聽sl);
if聽(uc聽<0)聽{
updatePositions(src,聽sp,聽dst,聽dp);
return聽sgp.error();
}
if聽(dl聽-聽dp聽<4)
return聽overflow(src,聽sp,聽dst,聽dp);
da[dp++]聽=聽(byte)(0xf0|聽((uc聽>>18)));
da[dp++]聽=聽(byte)(0x80|聽((uc聽>>12)聽&0x3f));
da[dp++]聽=聽(byte)(0x80|聽((uc聽>>6)聽&0x3f));
da[dp++]聽=聽(byte)(0x80|聽(uc聽&0x3f));
sp++;//聽2聽chars
}else聽{
//聽3聽bytes,聽16聽bits
if聽(dl聽-聽dp聽<3)
return聽overflow(src,聽sp,聽dst,聽dp);
da[dp++]聽=聽(byte)(0xe0|聽((c聽>>12)));
da[dp++]聽=聽(byte)(0x80|聽((c聽>>6)聽&0x3f));
da[dp++]聽=聽(byte)(0x80|聽(c聽&0x3f));
}
sp++;
}
updatePositions(src,聽sp,聽dst,聽dp);
return聽CoderResult.UNDERFLOW;
}
UTF-8 缂栫爜涓� GBK 鍜� GB2312 涓嶅悓锛屼笉鐢ㄦ煡鐮佽〃锛屾墍浠ュ湪缂栫爜鏁堢巼涓� UTF-8 鐨勬晥鐜囦細鏇村ソ锛屾墍浠ュ湪瀛樺偍涓枃瀛楃鏃� UTF-8 缂栫爜姣旇緝鐞嗘兂銆�
鍑犵缂栫爜鏍煎紡鐨勬瘮杈�
瀵逛腑鏂囧瓧绗﹀悗闈㈠洓绉嶇紪鐮佹牸寮忛兘鑳藉鐞嗭紝GB2312 涓� GBK 缂栫爜瑙勫垯绫讳技锛屼絾鏄� GBK 鑼冨洿鏇村ぇ锛屽畠鑳藉鐞嗘墍鏈夋眽瀛楀瓧绗︼紝鎵�浠� GB2312 涓� GBK 姣旇緝搴旇閫夋嫨 GBK銆俇TF-16 涓� UTF-8 閮芥槸澶勭悊 Unicode 缂栫爜锛屽畠浠殑缂栫爜瑙勫垯涓嶅お鐩稿悓锛岀浉瀵规潵璇� UTF-16 缂栫爜鏁堢巼鏈�楂橈紝瀛楃鍒板瓧鑺傜浉浜掕浆鎹㈡洿绠�鍗曪紝杩涜瀛楃涓叉搷浣滀篃鏇村ソ銆傚畠閫傚悎鍦ㄦ湰鍦扮鐩樺拰鍐呭瓨涔嬮棿浣跨敤锛屽彲浠ヨ繘琛屽瓧绗﹀拰瀛楄妭涔嬮棿蹇�熷垏鎹紝濡� Java 鐨勫唴瀛樼紪鐮佸氨鏄噰鐢� UTF-16 缂栫爜銆備絾鏄畠涓嶉�傚悎鍦ㄧ綉缁滀箣闂翠紶杈擄紝鍥犱负缃戠粶浼犺緭瀹规槗鎹熷潖瀛楄妭娴侊紝涓�鏃﹀瓧鑺傛祦鎹熷潖灏嗗緢闅炬仮澶嶏紝鎯虫瘮杈冭�岃█ UTF-8 鏇撮�傚悎缃戠粶浼犺緭锛屽 ASCII 瀛楃閲囩敤鍗曞瓧鑺傚瓨鍌紝鍙﹀鍗曚釜瀛楃鎹熷潖涔熶笉浼氬奖鍝嶅悗闈㈠叾瀹冨瓧绗︼紝鍦ㄧ紪鐮佹晥鐜囦笂浠嬩簬 GBK 鍜� UTF-16 涔嬮棿锛屾墍浠� UTF-8 鍦ㄧ紪鐮佹晥鐜囦笂鍜岀紪鐮佸畨鍏ㄦ�т笂鍋氫簡骞宠 锛屾槸鐞嗘兂鐨勪腑鏂囩紪鐮佹柟寮忋��
Java Web 娑夊強鍒扮殑缂栫爜
瀵逛簬浣跨敤涓枃鏉ヨ锛屾湁 I/O 鐨勫湴鏂瑰氨浼氭秹鍙婂埌缂栫爜锛屽墠闈㈠凡缁忔彁鍒颁簡 I/O 鎿嶄綔浼氬紩璧风紪鐮侊紝鑰屽ぇ閮ㄥ垎 I/O 寮曡捣鐨勪贡鐮侀兘鏄綉缁� I/O锛屽洜涓虹幇鍦ㄥ嚑涔庢墍鏈夌殑搴旂敤绋嬪簭閮芥秹鍙婂埌缃戠粶鎿嶄綔锛岃�屾暟鎹粡杩囩綉缁滀紶杈撻兘鏄互瀛楄妭涓哄崟浣嶇殑锛屾墍浠ユ墍鏈夌殑鏁版嵁閮藉繀椤昏兘澶熻搴忓垪鍖栦负瀛楄妭銆傚湪 Java 涓暟鎹搴忓垪鍖栧繀椤荤户鎵� Serializable 鎺ュ彛銆�
杩欓噷鏈変竴涓棶棰橈紝浣犳槸鍚﹁鐪熻�冭檻杩囦竴娈垫枃鏈畠鐨勫疄闄呭ぇ灏忓簲璇ユ�庝箞璁$畻锛屾垜鏇剧粡纰板埌杩囦竴涓棶棰橈細灏辨槸瑕佹兂鍔炴硶鍘嬬缉 Cookie 澶у皬锛屽噺灏戠綉缁滀紶杈撻噺锛屽綋鏃舵湁閫夋嫨涓嶅悓鐨勫帇缂╃畻娉曪紝鍙戠幇鍘嬬缉鍚庡瓧绗︽暟鏄噺灏戜簡锛屼絾鏄苟娌℃湁鍑忓皯瀛楄妭鏁般�傛墍璋撶殑鍘嬬缉鍙槸灏嗗涓崟瀛楄妭瀛楃閫氳繃缂栫爜杞彉鎴愪竴涓瀛楄妭瀛楃銆傚噺灏戠殑鏄� String.length()锛岃�屽苟娌℃湁鍑忓皯鏈�缁堢殑瀛楄妭鏁般�備緥濡傚皢鈥渁b鈥濅袱涓瓧绗﹂�氳繃鏌愮缂栫爜杞彉鎴愪竴涓鎬殑瀛楃锛岃櫧鐒跺瓧绗︽暟浠庝袱涓彉鎴愪竴涓紝浣嗘槸濡傛灉閲囩敤 UTF-8 缂栫爜杩欎釜濂囨�殑瀛楃鏈�鍚庣粡杩囩紪鐮佸彲鑳藉張浼氬彉鎴愪笁涓垨鏇村鐨勫瓧鑺傘�傚悓鏍风殑閬撶悊姣斿鏁村瀷鏁板瓧 1234567 濡傛灉褰撴垚瀛楃鏉ュ瓨鍌紝閲囩敤 UTF-8 鏉ョ紪鐮佸崰鐢� 7 涓� byte锛岄噰鐢� UTF-16 缂栫爜灏嗕細鍗犵敤 14 涓� byte锛屼絾鏄妸瀹冨綋鎴� int 鍨嬫暟瀛楁潵瀛樺偍鍙渶瑕� 4 涓� byte 鏉ュ瓨鍌ㄣ�傛墍浠ョ湅涓�娈垫枃鏈殑澶у皬锛岀湅瀛楃鏈韩鐨勯暱搴︽槸娌℃湁鎰忎箟鐨勶紝鍗充娇鏄竴鏍风殑瀛楃閲囩敤涓嶅悓鐨勭紪鐮佹渶缁堝瓨鍌ㄧ殑澶у皬涔熶細涓嶅悓锛屾墍浠ヤ粠瀛楃鍒板瓧鑺備竴瀹氳鐪嬬紪鐮佺被鍨嬨��
鍙﹀涓�涓棶棰橈紝浣犳槸鍚﹁�冭檻杩囷紝褰撴垜浠湪鐢佃剳涓煇涓枃鏈紪杈戝櫒閲岃緭鍏ユ煇涓眽瀛楁椂锛屽畠鍒板簳鏄�庝箞琛ㄧず鐨勶紵鎴戜滑鐭ラ亾锛岃绠楁満閲屾墍鏈夌殑淇℃伅閮芥槸浠� 01 琛ㄧず鐨勶紝閭d箞涓�涓眽瀛楋紝瀹冨埌搴曟槸澶氬皯涓� 0 鍜� 1 鍛紵鎴戜滑鑳藉鐪嬪埌鐨勬眽瀛楅兘鏄互瀛楃褰㈠紡鍑虹幇鐨勶紝渚嬪鍦� Java 涓�滄窐瀹濃�濅袱涓瓧绗︼紝瀹冨湪璁$畻鏈轰腑鐨勬暟鍊� 10 杩涘埗鏄� 28120 鍜� 23453锛�16 杩涘埗鏄� 6bd8 鍜� 5d9d锛屼篃灏辨槸杩欎袱涓瓧绗︽槸鐢辫繖涓や釜鏁板瓧鍞竴琛ㄧず鐨勩�侸ava 涓竴涓� char 鏄� 16 涓� bit 鐩稿綋浜庝袱涓瓧鑺傦紝鎵�浠ヤ袱涓眽瀛楃敤 char 琛ㄧず鍦ㄥ唴瀛樹腑鍗犵敤鐩稿綋浜庡洓涓瓧鑺傜殑绌洪棿銆�
杩欎袱涓棶棰樻悶娓呮鍚庯紝鎴戜滑鐪嬩竴涓� Java Web 涓偅浜涘湴鏂瑰彲鑳戒細瀛樺湪缂栫爜杞崲锛�
鐢ㄦ埛浠庢祻瑙堝櫒绔彂璧蜂竴涓� HTTP 璇锋眰锛岄渶瑕佸瓨鍦ㄧ紪鐮佺殑鍦版柟鏄� URL銆丆ookie銆丳arameter銆傛湇鍔″櫒绔帴鍙楀埌 HTTP 璇锋眰鍚庤瑙f瀽 HTTP 鍗忚锛屽叾涓� URI銆丆ookie 鍜� POST 琛ㄥ崟鍙傛暟闇�瑕佽В鐮侊紝鏈嶅姟鍣ㄧ鍙兘杩橀渶瑕佽鍙栨暟鎹簱涓殑鏁版嵁锛屾湰鍦版垨缃戠粶涓叾瀹冨湴鏂圭殑鏂囨湰鏂囦欢锛岃繖浜涙暟鎹兘鍙兘瀛樺湪缂栫爜闂锛屽綋 Servlet 澶勭悊瀹屾墍鏈夎姹傜殑鏁版嵁鍚庯紝闇�瑕佸皢杩欎簺鏁版嵁鍐嶇紪鐮侀�氳繃 Socket 鍙戦�佸埌鐢ㄦ埛璇锋眰鐨勬祻瑙堝櫒閲岋紝鍐嶇粡杩囨祻瑙堝櫒瑙g爜鎴愪负鏂囨湰銆傝繖浜涜繃绋嬪涓嬪浘鎵�绀猴細
濡備笂鍥炬墍绀轰竴娆� HTTP 璇锋眰璁捐鍒板緢澶氬湴鏂归渶瑕佺紪瑙g爜锛屽畠浠紪瑙g爜鐨勮鍒欐槸浠�涔堬紵涓嬮潰灏嗕細閲嶇偣闃愯堪涓�涓嬶細
URL 鐨勭紪瑙g爜
鐢ㄦ埛鎻愪氦涓�涓� URL锛岃繖涓� URL 涓彲鑳藉瓨鍦ㄤ腑鏂囷紝鍥犳闇�瑕佺紪鐮侊紝濡備綍瀵硅繖涓� URL 杩涜缂栫爜锛熸牴鎹粈涔堣鍒欐潵缂栫爜锛熸湁濡備綍鏉ヨВ鐮侊紵濡備笅鍥句竴涓� URL锛�
鍥� 4.URL 鐨勫嚑涓粍鎴愰儴鍒�
涓婂浘涓互 Tomcat 浣滀负 Servlet Engine 涓轰緥锛屽畠浠垎鍒搴斿埌涓嬮潰杩欎簺閰嶇疆鏂囦欢涓細
Port 瀵瑰簲鍦� Tomcat 鐨� 涓厤缃紝鑰� Context Path 鍦� 涓厤缃紝Servlet Path 鍦� Web 搴旂敤鐨� web.xml 涓殑
junshanExample
/servlets/servlet/*
涓厤缃紝PathInfo 鏄垜浠姹傜殑鍏蜂綋鐨� Servlet锛孮ueryString 鏄浼犻�掔殑鍙傛暟锛屾敞鎰忚繖閲屾槸鍦ㄦ祻瑙堝櫒閲岀洿鎺ヨ緭鍏� URL 鎵�浠ユ槸閫氳繃 Get 鏂规硶璇锋眰鐨勶紝濡傛灉鏄� POST 鏂规硶璇锋眰鐨勮瘽锛孮ueryString 灏嗛�氳繃琛ㄥ崟鏂瑰紡鎻愪氦鍒版湇鍔″櫒绔紝杩欎釜灏嗗湪鍚庨潰鍐嶄粙缁嶃��
涓婂浘涓� PathInfo 鍜� QueryString 鍑虹幇浜嗕腑鏂囷紝褰撴垜浠湪娴忚鍣ㄤ腑鐩存帴杈撳叆杩欎釜 URL 鏃讹紝鍦ㄦ祻瑙堝櫒绔拰鏈嶅姟绔細濡備綍缂栫爜鍜岃В鏋愯繖涓� URL 鍛紵涓轰簡楠岃瘉娴忚鍣ㄦ槸鎬庝箞缂栫爜 URL 鐨勬垜浠�夋嫨 FireFox 娴忚鍣ㄥ苟閫氳繃 HTTPFox 鎻掍欢瑙傚療鎴戜滑璇锋眰鐨� URL 鐨勫疄闄呯殑鍐呭锛屼互涓嬫槸 URL锛欻TTP://localhost:8080/examples/servlets/servlet/ 鍚涘北 ?author= 鍚涘北鍦ㄤ腑鏂� FireFox3.6.12 鐨勬祴璇曠粨鏋�