ºìÁªLinuxÃÅ»§
Linux°ïÖú

java»ù´¡ -- JAVA×Ö·û¼¯

·¢²¼Ê±¼ä:2006-11-10 00:31:55À´Ô´:ºìÁª×÷Õß:Joyful
1. ¸ÅÊö

±¾ÎÄÖ÷Òª°üÀ¨ÒÔϼ¸¸ö·½Ã棺±àÂë»ù±¾ÖªÊ¶£¬java£¬ÏµÍ³Èí¼þ£¬url£¬¹¤¾ßÈí¼þµÈ¡£

ÔÚÏÂÃæµÄÃèÊöÖУ¬½«ÒÔ"ÖÐÎÄ"Á½¸ö×ÖΪÀý£¬¾­²é±í¿ÉÒÔÖªµÀÆäGB2312±àÂëÊÇ"d6d0 cec4"£¬Unicode±àÂëΪ"4e2d 6587"£¬UTF±àÂë¾ÍÊÇ"e4b8ad e69687"¡£×¢Ò⣬ÕâÁ½¸ö×ÖûÓÐiso8859-1±àÂ룬µ«¿ÉÒÔÓÃiso8859-1±àÂëÀ´"±íʾ"¡£

2. ±àÂë»ù±¾ÖªÊ¶

×îÔçµÄ±àÂëÊÇiso8859-1£¬ºÍascii±àÂëÏàËÆ¡£µ«ÎªÁË·½±ã±íʾ¸÷ÖÖ¸÷ÑùµÄÓïÑÔ£¬Öð½¥³öÏÖÁ˺ܶà±ê×¼±àÂ룬ÖØÒªµÄÓÐÈçϼ¸¸ö¡£

2.1. iso8859-1

ÊôÓÚµ¥×Ö½Ú±àÂ룬×î¶àÄܱíʾµÄ×Ö·û·¶Î§ÊÇ0-255£¬Ó¦ÓÃÓÚÓ¢ÎÄϵÁС£±ÈÈ磬×Öĸ'a'µÄ±àÂëΪ0x61=97¡£

ºÜÃ÷ÏÔ£¬iso8859-1±àÂë±íʾµÄ×Ö·û·¶Î§ºÜÕ­£¬ÎÞ·¨±íʾÖÐÎÄ×Ö·û¡£µ«ÊÇ£¬ÓÉÓÚÊǵ¥×Ö½Ú±àÂ룬ºÍ¼ÆËã»ú×î»ù´¡µÄ±íʾµ¥Î»Ò»Ö£¬ËùÒԺܶàʱºò£¬ÈÔ¾ÉʹÓÃiso8859-1±àÂëÀ´±íʾ¡£¶øÇÒÔںܶàЭÒéÉÏ£¬Ä¬ÈÏʹÓøñàÂë¡£±ÈÈ磬ËäÈ»"ÖÐÎÄ"Á½¸ö×Ö²»´æÔÚiso8859-1±àÂ룬ÒÔgb2312±àÂëΪÀý£¬Ó¦¸ÃÊÇ"d6d0 cec4"Á½¸ö×Ö·û£¬Ê¹ÓÃiso8859-1±àÂëµÄʱºòÔò½«Ëü²ð¿ªÎª4¸ö×Ö½ÚÀ´±íʾ£º"d6 d0 ce c4"£¨ÊÂʵÉÏ£¬ÔÚ½øÐд洢µÄʱºò£¬Ò²ÊÇÒÔ×Ö½ÚΪµ¥Î»´¦ÀíµÄ£©¡£¶øÈç¹ûÊÇUTF±àÂ룬ÔòÊÇ6¸ö×Ö½Ú"e4 b8 ad e6 96 87"¡£ºÜÃ÷ÏÔ£¬ÕâÖÖ±íʾ·½·¨»¹ÐèÒªÒÔÁíÒ»ÖÖ±àÂëΪ»ù´¡¡£

2.2. GB2312/GBK

Õâ¾ÍÊǺº×ӵĹú±êÂ룬רÃÅÓÃÀ´±íʾºº×Ö£¬ÊÇË«×Ö½Ú±àÂ룬¶øÓ¢ÎÄ×ÖĸºÍiso8859-1Ò»Ö£¨¼æÈÝiso8859-1±àÂ룩¡£ÆäÖÐgbk±àÂëÄܹ»ÓÃÀ´Í¬Ê±±íʾ·±Ìå×ֺͼòÌå×Ö£¬¶øgb2312Ö»Äܱíʾ¼òÌå×Ö£¬gbkÊǼæÈÝgb2312±àÂëµÄ¡£

2.3. unicode

ÕâÊÇ×îͳһµÄ±àÂ룬¿ÉÒÔÓÃÀ´±íʾËùÓÐÓïÑÔµÄ×Ö·û£¬¶øÇÒÊǶ¨³¤Ë«×Ö½Ú£¨Ò²ÓÐËÄ×ֽڵģ©±àÂ룬°üÀ¨Ó¢ÎÄ×ÖĸÔÚÄÚ¡£ËùÒÔ¿ÉÒÔ˵ËüÊDz»¼æÈÝiso8859-1±àÂëµÄ£¬Ò²²»¼æÈÝÈκαàÂë¡£²»¹ý£¬Ïà¶ÔÓÚiso8859-1±àÂëÀ´Ëµ£¬uniocode±àÂëÖ»ÊÇÔÚÇ°ÃæÔö¼ÓÁËÒ»¸ö0×Ö½Ú£¬±ÈÈç×Öĸ'a'Ϊ"00 61"¡£

ÐèҪ˵Ã÷µÄÊÇ£¬¶¨³¤±àÂë±ãÓÚ¼ÆËã»ú´¦Àí£¨×¢ÒâGB2312/GBK²»ÊǶ¨³¤±àÂ룩£¬¶øunicodeÓÖ¿ÉÒÔÓÃÀ´±íʾËùÓÐ×Ö·û£¬ËùÒÔÔںܶàÈí¼þÄÚ²¿ÊÇʹÓÃunicode±àÂëÀ´´¦ÀíµÄ£¬±ÈÈçjava¡£

2.4. UTF

¿¼Âǵ½unicode±àÂë²»¼æÈÝiso8859-1±àÂ룬¶øÇÒÈÝÒ×Õ¼Óøü¶àµÄ¿Õ¼ä£ºÒòΪ¶ÔÓÚÓ¢ÎÄ×Öĸ£¬unicodeÒ²ÐèÒªÁ½¸ö×Ö½ÚÀ´±íʾ¡£ËùÒÔunicode²»±ãÓÚ´«ÊäºÍ´æ´¢¡£Òò´Ë¶ø²úÉúÁËutf±àÂ룬utf±àÂë¼æÈÝiso8859-1±àÂ룬ͬʱҲ¿ÉÒÔÓÃÀ´±íʾËùÓÐÓïÑÔµÄ×Ö·û£¬²»¹ý£¬utf±àÂëÊDz»¶¨³¤±àÂ룬ÿһ¸ö×Ö·ûµÄ³¤¶È´Ó1-6¸ö×Ö½Ú²»µÈ¡£ÁíÍ⣬utf±àÂë×Ô´ø¼òµ¥µÄУÑ鹦ÄÜ¡£Ò»°ãÀ´½²£¬Ó¢ÎÄ×Öĸ¶¼ÊÇÓÃÒ»¸ö×Ö½Ú±íʾ£¬¶øºº×ÖʹÓÃÈý¸ö×Ö½Ú¡£

×¢Ò⣬ËäȻ˵utfÊÇΪÁËʹÓøüÉٵĿռä¶øʹÓõģ¬µ«ÄÇÖ»ÊÇÏà¶ÔÓÚunicode±àÂëÀ´Ëµ£¬Èç¹ûÒѾ­ÖªµÀÊǺº×Ö£¬ÔòʹÓÃGB2312/GBKÎÞÒÉÊÇ×î½ÚÊ¡µÄ¡£²»¹ýÁíÒ»·½Ã棬ֵµÃ˵Ã÷µÄÊÇ£¬ËäÈ»utf±àÂë¶Ôºº×ÖʹÓÃ3¸ö×Ö½Ú£¬µ«¼´Ê¹¶ÔÓÚºº×ÖÍøÒ³£¬utf±àÂëÒ²»á±Èunicode±àÂë½ÚÊ¡£¬ÒòΪÍøÒ³Öаüº¬Á˺ܶàµÄÓ¢ÎÄ×Ö·û¡£

3. java¶Ô×Ö·ûµÄ´¦Àí

ÔÚjavaÓ¦ÓÃÈí¼þÖУ¬»áÓжദÉæ¼°µ½×Ö·û¼¯±àÂ룬ÓÐЩµØ·½ÐèÒª½øÐÐÕýÈ·µÄÉèÖã¬ÓÐЩµØ·½ÐèÒª½øÐÐÒ»¶¨³Ì¶ÈµÄ´¦Àí¡£

3.1. getBytes(charset)

ÕâÊÇjava×Ö·û´®´¦ÀíµÄÒ»¸ö±ê×¼º¯Êý£¬Æä×÷ÓÃÊǽ«×Ö·û´®Ëù±íʾµÄ×Ö·û°´ÕÕcharset±àÂ룬²¢ÒÔ×Ö½Ú·½Ê½±íʾ¡£×¢Òâ×Ö·û´®ÔÚjavaÄÚ´æÖÐ×ÜÊÇ°´unicode±àÂë´æ´¢µÄ¡£±ÈÈç"ÖÐÎÄ"£¬Õý³£Çé¿öÏ£¨¼´Ã»ÓдíÎóµÄʱºò£©´æ´¢Îª"4e2d 6587"£¬Èç¹ûcharsetΪ"gbk"£¬Ôò±»±àÂëΪ"d6d0 cec4"£¬È»ºó·µ»Ø×Ö½Ú"d6 d0 ce c4"¡£Èç¹ûcharsetΪ"utf8"Ôò×îºóÊÇ"e4 b8 ad e6 96 87"¡£Èç¹ûÊÇ"iso8859-1"£¬ÔòÓÉÓÚÎÞ·¨±àÂ룬×îºó·µ»Ø "3f 3f"£¨Á½¸öÎʺţ©¡£

3.2. new String(charset)

ÕâÊÇjava×Ö·û´®´¦ÀíµÄÁíÒ»¸ö±ê×¼º¯Êý£¬ºÍÉÏÒ»¸öº¯ÊýµÄ×÷ÓÃÏà·´£¬½«×Ö½ÚÊý×é°´ÕÕcharset±àÂë½øÐÐ×éºÏʶ±ð£¬×îºóת»»Îªunicode´æ´¢¡£²Î¿¼ÉÏÊögetBytesµÄÀý×Ó£¬"gbk" ºÍ"utf8"¶¼¿ÉÒԵóöÕýÈ·µÄ½á¹û"4e2d 6587"£¬µ«iso8859-1×îºó±ä³ÉÁË"003f 003f"£¨Á½¸öÎʺţ©¡£

ÒòΪutf8¿ÉÒÔÓÃÀ´±íʾ/±àÂëËùÓÐ×Ö·û£¬ËùÒÔnew String( str.getBytes( "utf8" ), "utf8" ) === str£¬¼´ÍêÈ«¿ÉÄæ¡£

3.3. setCharacterEncoding()

¸Ãº¯ÊýÓÃÀ´ÉèÖÃhttpÇëÇó»òÕßÏàÓ¦µÄ±àÂë¡£

¶ÔÓÚrequest£¬ÊÇÖ¸Ìá½»ÄÚÈݵıàÂ룬ָ¶¨ºó¿ÉÒÔͨ¹ýgetParameter()ÔòÖ±½Ó»ñµÃÕýÈ·µÄ×Ö·û´®£¬Èç¹û²»Ö¸¶¨£¬ÔòĬÈÏʹÓÃiso8859-1±àÂ룬ÐèÒª½øÒ»²½´¦Àí¡£²Î¼ûÏÂÊö"±íµ¥ÊäÈë"¡£ÖµµÃ×¢ÒâµÄÊÇÔÚÖ´ÐÐsetCharacterEncoding()֮ǰ£¬²»ÄÜÖ´ÐÐÈκÎgetParameter()¡£java docÉÏ˵Ã÷£ºThis method must be called prior to reading request parameters or reading input using getReader()¡£¶øÇÒ£¬¸ÃÖ¸¶¨Ö»¶ÔPOST·½·¨ÓÐЧ£¬¶ÔGET·½·¨ÎÞЧ¡£·ÖÎöÔ­Òò£¬Ó¦¸ÃÊÇÔÚÖ´ÐеÚÒ»¸ögetParameter()µÄʱºò£¬java½«»á°´ÕÕ±àÂë·ÖÎöËùÓеÄÌá½»ÄÚÈÝ£¬¶øºóÐøµÄgetParameter()²»ÔÙ½øÐзÖÎö£¬ËùÒÔsetCharacterEncoding()ÎÞЧ¡£¶ø¶ÔÓÚGET·½·¨Ìá½»±íµ¥ÊÇ£¬Ìá½»µÄÄÚÈÝÔÚURLÖУ¬Ò»¿ªÊ¼¾ÍÒѾ­°´ÕÕ±àÂë·ÖÎöËùÓеÄÌá½»ÄÚÈÝ£¬setCharacterEncoding()×ÔÈ»¾ÍÎÞЧ¡£

¶ÔÓÚresponse£¬ÔòÊÇÖ¸¶¨Êä³öÄÚÈݵıàÂ룬ͬʱ£¬¸ÃÉèÖûᴫµÝ¸øä¯ÀÀÆ÷£¬¸æËßä¯ÀÀÆ÷Êä³öÄÚÈÝËù²ÉÓõıàÂë¡£

3.4. ´¦Àí¹ý³Ì

ÏÂÃæ·ÖÎöÁ½¸öÓдú±íÐÔµÄÀý×Ó£¬ËµÃ÷java¶Ô±àÂëÓйØÎÊÌâµÄ´¦Àí·½·¨¡£

3.4.1. ±íµ¥ÊäÈë

User input *(gbk:d6d0 cec4) browser *(gbk:d6d0 cec4) web server iso8859-1(00d6 00d 000ce 00c4) class£¬ÐèÒªÔÚclassÖнøÐд¦Àí£ºgetbytes("iso8859-1")Ϊd6 d0 ce c4£¬new String("gbk")Ϊd6d0 cec4£¬ÄÚ´æÖÐÒÔunicode±àÂëÔòΪ4e2d 6587¡£

l Óû§ÊäÈëµÄ±àÂ뷽ʽºÍÒ³ÃæÖ¸¶¨µÄ±àÂëÓйأ¬Ò²ºÍÓû§µÄ²Ù×÷ϵͳÓйأ¬ËùÒÔÊDz»È·¶¨µÄ£¬ÉÏÀýÒÔgbkΪÀý¡£

l ´Óbrowserµ½web server£¬¿ÉÒÔÔÚ±íµ¥ÖÐÖ¸¶¨Ìá½»ÄÚÈÝʱʹÓõÄ×Ö·û¼¯£¬·ñÔò»áʹÓÃÒ³ÃæÖ¸¶¨µÄ±àÂë¡£¶øÈç¹ûÔÚurlÖÐÖ±½ÓÓÃ?µÄ·½Ê½ÊäÈë²ÎÊý£¬ÔòÆä±àÂëÍùÍùÊDzÙ×÷ϵͳ±¾ÉíµÄ±àÂ룬ÒòΪÕâʱºÍÒ³ÃæÎ޹ء£ÉÏÊöÈÔ¾ÉÒÔgbk±àÂëΪÀý¡£

l Web server½ÓÊÕµ½µÄÊÇ×Ö½ÚÁ÷£¬Ä¬ÈÏʱ£¨getParameter£©»áÒÔiso8859-1±àÂë´¦ÀíÖ®£¬½á¹ûÊDz»ÕýÈ·µÄ£¬ËùÒÔÐèÒª½øÐд¦Àí¡£µ«Èç¹ûÔ¤ÏÈÉèÖÃÁ˱àÂ루ͨ¹ýrequest. setCharacterEncoding ()£©£¬ÔòÄܹ»Ö±½Ó»ñÈ¡µ½ÕýÈ·µÄ½á¹û¡£

l ÔÚÒ³ÃæÖÐÖ¸¶¨±àÂëÊǸöºÃÏ°¹ß£¬·ñÔò¿ÉÄÜʧȥ¿ØÖÆ£¬ÎÞ·¨Ö¸¶¨ÕýÈ·µÄ±àÂë¡£

3.4.2. Îļþ±àÒë

¼ÙÉèÎļþÊÇgbk±àÂë±£´æµÄ£¬¶ø±àÒëÓÐÁ½ÖÖ±àÂëÑ¡Ôñ£ºgbk»òÕßiso8859-1£¬Ç°ÕßÊÇÖÐÎÄwindowsµÄĬÈϱàÂ룬ºóÕßÊÇlinuxµÄĬÈϱàÂ룬µ±È»Ò²¿ÉÒÔÔÚ±àÒëʱָ¶¨±àÂë¡£

Jsp *(gbk:d6d0 cec4) java file *(gbk:d6d0 cec4) compiler read uincode(gbk: 4e2d 6587; iso8859-1: 00d6 00d 000ce 00c4) compiler write utf(gbk: e4b8ad e69687; iso8859-1: *) compiled file unicode(gbk: 4e2d 6587; iso8859-1: 00d6 00d 000ce 00c4) class¡£ËùÒÔÓÃgbk±àÂë±£´æ£¬¶øÓÃiso8859-1±àÒëµÄ½á¹ûÊDz»ÕýÈ·µÄ¡£

class unicode(4e2d 6587) system.out / jsp.out gbk(d6d0 cec4) os console / browser¡£

l Îļþ¿ÉÒÔÒÔ¶àÖÖ±àÂ뷽ʽ±£´æ£¬ÖÐÎÄwindowsÏ£¬Ä¬ÈÏΪansi/gbk¡£

l ±àÒëÆ÷¶ÁÈ¡Îļþʱ£¬ÐèÒªµÃµ½ÎļþµÄ±àÂ룬Èç¹ûδָ¶¨£¬ÔòʹÓÃϵͳĬÈϱàÂë¡£Ò»°ãclassÎļþ£¬ÊÇÒÔϵͳĬÈϱàÂë±£´æµÄ£¬ËùÒÔ±àÒë²»»á³öÎÊÌ⣬µ«¶ÔÓÚjspÎļþ£¬Èç¹ûÔÚÖÐÎÄwindowsϱ༭±£´æ£¬¶ø²¿ÊðÔÚÓ¢ÎÄlinuxÏÂÔËÐÐ/±àÒ룬Ôò»á³öÏÖÎÊÌâ¡£ËùÒÔÐèÒªÔÚjspÎļþÖÐÓÃpageEncodingÖ¸¶¨±àÂë¡£

l Java±àÒëµÄʱºò»áת»»³ÉͳһµÄunicode±àÂë´¦Àí£¬×îºó±£´æµÄʱºòÔÙת»»Îªutf±àÂë¡£

l µ±ÏµÍ³Êä³ö×Ö·ûµÄʱºò£¬»á°´Ö¸¶¨±àÂëÊä³ö£¬¶ÔÓÚÖÐÎÄwindowsÏ£¬System.out½«Ê¹ÓÃgbk±àÂ룬¶ø¶ÔÓÚresponse£¨ä¯ÀÀÆ÷£©£¬ÔòʹÓÃjspÎļþÍ·Ö¸¶¨µÄcontentType£¬»òÕß¿ÉÒÔÖ±½ÓΪresponseÖ¸¶¨±àÂ롣ͬʱ£¬»á¸æËßbrowserÍøÒ³µÄ±àÂë¡£Èç¹ûδָ¶¨£¬Ôò»áʹÓÃiso8859-1±àÂë¡£¶ÔÓÚÖÐÎÄ£¬Ó¦¸ÃΪbrowserÖ¸¶¨Êä³ö×Ö·û´®µÄ±àÂë¡£

l browserÏÔʾÍøÒ³µÄʱºò£¬Ê×ÏÈʹÓÃresponseÖÐÖ¸¶¨µÄ±àÂ루jspÎļþÍ·Ö¸¶¨µÄcontentType×îÖÕÒ²·´Ó³ÔÚresponseÉÏ£©£¬Èç¹ûδָ¶¨£¬Ôò»áʹÓÃÍøÒ³ÖÐmetaÏîÖ¸¶¨ÖеÄcontentType¡£

3.5. ¼¸´¦ÉèÖÃ

¶ÔÓÚwebÓ¦ÓóÌÐò£¬ºÍ±àÂëÓйصÄÉèÖûòÕߺ¯ÊýÈçÏ¡£

3.5.1. jsp±àÒë

Ö¸¶¨ÎļþµÄ´æ´¢±àÂ룬ºÜÃ÷ÏÔ£¬¸ÃÉèÖÃÓ¦¸ÃÖÃÓÚÎļþµÄ¿ªÍ·¡£ÀýÈ磺¡£ÁíÍ⣬¶ÔÓÚÒ»°ãclassÎļþ£¬¿ÉÒÔÔÚ±àÒëµÄʱºòÖ¸¶¨±àÂë¡£

3.5.2. jspÊä³ö

Ö¸¶¨ÎļþÊä³öµ½browserÊÇʹÓõıàÂ룬¸ÃÉèÖÃÒ²Ó¦¸ÃÖÃÓÚÎļþµÄ¿ªÍ·¡£ÀýÈ磺¡£¸ÃÉèÖúÍresponse.setCharacterEncoding("GBK")µÈЧ¡£

3.5.3. metaÉèÖÃ

Ö¸¶¨ÍøҳʹÓõıàÂ룬¸ÃÉèÖöԾ²Ì¬ÍøÒ³ÓÈÆäÓÐ×÷Óá£ÒòΪ¾²Ì¬ÍøÒ³ÎÞ·¨²ÉÓÃjspµÄÉèÖ㬶øÇÒÒ²ÎÞ·¨Ö´ÐÐresponse.setCharacterEncoding()¡£ÀýÈ磺

Èç¹ûͬʱ²ÉÓÃÁËjspÊä³öºÍmetaÉèÖÃÁ½ÖÖ±àÂëÖ¸¶¨·½Ê½£¬ÔòjspÖ¸¶¨µÄÓÅÏÈ¡£ÒòΪjspÖ¸¶¨µÄÖ±½ÓÌåÏÖÔÚresponseÖС£

ÐèҪעÒâµÄÊÇ£¬apacheÓÐÒ»¸öÉèÖÿÉÒÔ¸øÎÞ±àÂëÖ¸¶¨µÄÍøÒ³Ö¸¶¨±àÂ룬¸ÃÖ¸¶¨µÈͬÓÚjspµÄ±àÂëÖ¸¶¨·½Ê½£¬ËùÒԻḲ¸Ç¾²Ì¬ÍøÒ³ÖеÄmetaÖ¸¶¨¡£ËùÒÔÓÐÈ˽¨Òé¹Ø±Õ¸ÃÉèÖá£

3.5.4. formÉèÖÃ

µ±ä¯ÀÀÆ÷Ìá½»±íµ¥µÄʱºò£¬¿ÉÒÔÖ¸¶¨ÏàÓ¦µÄ±àÂë¡£ÀýÈ磺¡£Ò»°ã²»±Ø²»Ê¹ÓøÃÉèÖã¬ä¯ÀÀÆ÷»áÖ±½ÓʹÓÃÍøÒ³µÄ±àÂë¡£

4. ϵͳÈí¼þ

ÏÂÃæÌÖÂÛ¼¸¸öÏà¹ØµÄϵͳÈí¼þ¡£

4.1. mysqlÊý¾Ý¿â

ºÜÃ÷ÏÔ£¬ÒªÖ§³Ö¶àÓïÑÔ£¬Ó¦¸Ã½«Êý¾Ý¿âµÄ±àÂëÉèÖóÉutf»òÕßunicode£¬¶øutf¸üÊʺÏÓë´æ´¢¡£µ«ÊÇ£¬Èç¹ûÖÐÎÄÊý¾ÝÖаüº¬µÄÓ¢ÎÄ×ÖĸºÜÉÙ£¬Æäʵunicode¸üΪÊʺϡ£

Êý¾Ý¿âµÄ±àÂë¿ÉÒÔͨ¹ýmysqlµÄÅäÖÃÎļþÉèÖã¬ÀýÈçdefault-character-set=utf8¡£»¹¿ÉÒÔÔÚÊý¾Ý¿âÁ´½ÓURLÖÐÉèÖã¬ÀýÈ磺 useUnicode=true&characterEncoding=UTF-8¡£×¢ÒâÕâÁ½ÕßÓ¦¸Ã±£³ÖÒ»Ö£¬ÔÚеÄsql°æ±¾ÀÔÚÊý¾Ý¿âÁ´½ÓURLÀï¿ÉÒÔ²»½øÐÐÉèÖ㬵«Ò²²»ÄÜÊÇ´íÎóµÄÉèÖá£

4.2. apache

appacheºÍ±àÂëÓйصÄÅäÖÃÔÚhttpd.confÖУ¬ÀýÈçAddDefaultCharset UTF-8¡£ÈçÇ°ËùÊö£¬¸Ã¹¦ÄܻὫËùÓо²Ì¬Ò³ÃæµÄ±àÂëÉèÖÃΪUTF-8£¬×îºÃ¹Ø±Õ¸Ã¹¦ÄÜ¡£

ÁíÍ⣬apache»¹Óе¥¶ÀµÄÄ£¿éÀ´´¦ÀíÍøÒ³ÏìӦͷ£¬ÆäÖÐÒ²¿ÉÄܶԱàÂë½øÐÐÉèÖá£

4.3. linuxĬÈϱàÂë

ÕâÀïËù˵µÄlinuxĬÈϱàÂ룬ÊÇÖ¸ÔËÐÐʱµÄ»·¾³±äÁ¿¡£Á½¸öÖØÒªµÄ»·¾³±äÁ¿ÊÇLC_ALLºÍLANG£¬Ä¬ÈϱàÂë»áÓ°Ïìµ½java URLEncodeµÄÐÐΪ£¬ÏÂÃæÓÐÃèÊö¡£

½¨Ò鶼ÉèÖÃΪ"zh_CN.UTF-8"¡£

4.4. ÆäËü

ΪÁËÖ§³ÖÖÐÎÄÎļþÃû£¬linuxÔÚ¼ÓÔØ´ÅÅÌʱӦ¸ÃÖ¸¶¨×Ö·û¼¯£¬ÀýÈ磺mount /dev/hda5 /mnt/hda5/ -t ntfs -o iocharset=gb2312¡£

ÁíÍ⣬ÈçÇ°ËùÊö£¬Ê¹ÓÃGET·½·¨Ìá½»µÄÐÅÏ¢²»Ö§³Örequest.setCharacterEncoding()£¬µ«¿ÉÒÔͨ¹ýtomcatµÄÅäÖÃÎļþÖ¸¶¨×Ö·û¼¯£¬ÔÚtomcatµÄserver.xmlÎļþÖУ¬ÐÎÈ磺¡£ÕâÖÖ·½·¨½«Í³Ò»ÉèÖÃËùÓÐÇëÇ󣬶ø²»ÄÜÕë¶Ô¾ßÌåÒ³Ãæ½øÐÐÉèÖã¬Ò²²»Ò»¶¨ºÍbrowserʹÓõıàÂëÏàͬ£¬ËùÒÔÓÐʱºò²¢²»ÊÇËùÆÚÍûµÄ¡£

5. URLµØÖ·

URLµØÖ·Öк¬ÓÐÖÐÎÄ×Ö·ûÊǺÜÂé·³µÄ£¬Ç°ÃæÃèÊö¹ýʹÓÃGET·½·¨Ìá½»±íµ¥µÄÇé¿ö£¬Ê¹ÓÃGET·½·¨Ê±£¬²ÎÊý¾ÍÊÇ°üº¬ÔÚURLÖС£

5.1. URL±àÂë

¶ÔÓÚURLÖеÄһЩÌØÊâ×Ö·û£¬ä¯ÀÀÆ÷»á×Ô¶¯½øÐбàÂë¡£ÕâЩ×Ö·û³ýÁË"/?&"µÈÍ⣬»¹°üÀ¨unicode×Ö·û£¬±ÈÈ纺×Ó¡£ÕâʱµÄ±àÂë±È½ÏÌØÊâ¡£

IEÓÐÒ»¸öÑ¡Ïî"×ÜÊÇʹÓÃUTF-8·¢ËÍURL"£¬µ±¸ÃÑ¡ÏîÓÐЧʱ£¬IE½«»á¶ÔÌØÊâ×Ö·û½øÐÐUTF-8±àÂ룬ͬʱ½øÐÐURL±àÂë¡£Èç¹û¸ÄÑ¡ÏîÎÞЧ£¬ÔòʹÓÃĬÈϱàÂë"GBK"£¬²¢ÇÒ²»½øÐÐURL±àÂë¡£µ«ÊÇ£¬¶ÔÓÚURLºóÃæµÄ²ÎÊý£¬Ôò×ÜÊDz»½øÐбàÂ룬Ï൱ÓÚUTF-8Ñ¡ÏîÎÞЧ¡£±ÈÈç"ÖÐÎÄ.html?a=ÖÐÎÄ"£¬µ±UTF-8Ñ¡ÏîÓÐЧʱ£¬½«·¢ËÍÁ´½Ó"%e4%b8%ad%e6%96%87.html?a=\x4e\x2d\x65\x87"£»¶øUTF-8Ñ¡ÏîÎÞЧʱ£¬½«·¢ËÍÁ´½Ó"\x4e\x2d\x65\x87.html?a=\x4e\x2d\x65\x87"¡£×¢ÒâºóÕßÇ°ÃæµÄ"ÖÐÎÄ"Á½¸ö×ÖÖ»ÓÐ4¸ö×Ö½Ú£¬¶øÇ°ÕßÈ´ÓÐ18¸ö×Ö½Ú£¬ÕâÖ÷ҪʱURL±àÂëµÄÔ­Òò¡£

µ±web server£¨tomcat£©½ÓÊÕµ½¸ÃÁ´½Óʱ£¬½«»á½øÐÐURL½âÂ룬¼´È¥µô"%"£¬Í¬Ê±°´ÕÕISO8859-1±àÂ루ÉÏÃæÒѾ­ÃèÊö£¬¿ÉÒÔʹÓÃURLEncodingÀ´ÉèÖóÉÆäËü±àÂ룩ʶ±ð¡£ÉÏÊöÀý×ӵĽá¹û·Ö±ðÊÇ"\ue4\ub8\uad\ue6\u96\u87.html?a=\u4e\u2d\u65\u87"ºÍ"\u4e\u2d\u65\u87.html?a=\u4e\u2d\u65\u87"£¬×¢ÒâÇ°ÕßÇ°ÃæµÄ"ÖÐÎÄ"Á½¸ö×Ö»Ö¸´³ÉÁË6¸ö×Ö·û¡£ÕâÀïÓÃ"\u"£¬±íʾÊÇunicode¡£

ËùÒÔ£¬ÓÉÓÚ¿Í»§¶ËÉèÖõIJ»Í¬£¬ÏàͬµÄÁ´½Ó£¬ÔÚ·þÎñÆ÷Éϵõ½Á˲»Í¬½á¹û¡£Õâ¸öÎÊÌâ²»ÉÙÈ˶¼Óöµ½£¬È´Ã»ÓкܺõĽâ¾ö°ì·¨¡£ËùÒÔÓеÄÍøÕ¾»á½¨ÒéÓû§³¢ÊԹرÕUTF-8Ñ¡Ïî¡£²»¹ý£¬ÏÂÃæ»áÃèÊöÒ»¸ö¸üºÃµÄ´¦Àí°ì·¨¡£

5.2. rewrite

ÊìϤµÄÈ˶¼ÖªµÀ£¬apacheÓÐÒ»¸ö¹¦ÄÜÇ¿´óµÄrewriteÄ£¿é£¬ÕâÀï²»ÃèÊöÆ书ÄÜ¡£ÐèҪ˵Ã÷µÄÊǸÃÄ£¿é»á×Ô¶¯½«URL½âÂ루ȥ³ý%£©£¬¼´Íê³ÉÉÏÊöweb server£¨tomcat£©µÄ²¿·Ö¹¦ÄÜ¡£ÓÐÏà¹ØÎĵµ½éÉÜ˵¿ÉÒÔʹÓÃ[NE]²ÎÊýÀ´¹Ø±Õ¸Ã¹¦ÄÜ£¬µ«ÎÒÊÔÑ鲢δ³É¹¦£¬¿ÉÄÜÊÇÒòΪ°æ±¾£¨ÎÒʹÓõÄÊÇapache 2.0.54£©ÎÊÌâ¡£ÁíÍ⣬µ±²ÎÊýÖк¬ÓÐ"?& "µÈ·ûºÅµÄʱºò£¬¸Ã¹¦Äܽ«µ¼ÖÂϵͳµÃ²»µ½Õý³£½á¹û¡£

rewrite±¾ÉíËƺõÍêÈ«ÊDzÉÓÃ×Ö½Ú´¦ÀíµÄ·½Ê½£¬¶ø²»¿¼ÂÇ×Ö·û´®µÄ±àÂ룬ËùÒÔ²»»á´øÀ´±àÂëÎÊÌâ¡£

5.3. URLEncode.encode()

ÕâÊÇJava±¾ÉíÌṩ¶ÔµÄURL±àÂ뺯Êý£¬Íê³ÉµÄ¹¤×÷ºÍÉÏÊöUTF-8Ñ¡ÏîÓÐЧʱä¯ÀÀÆ÷Ëù×öµÄ¹¤×÷ÏàËÆ¡£ÖµµÃ˵Ã÷µÄÊÇ£¬javaÒѾ­²»Ô޳ɲ»Ö¸¶¨±àÂëÀ´Ê¹Óø÷½·¨£¨deprecated£©¡£Ó¦¸ÃÔÚʹÓõÄʱºòÔö¼Ó±àÂëÖ¸¶¨¡£

µ±²»Ö¸¶¨±àÂëµÄʱºò£¬¸Ã·½·¨Ê¹ÓÃϵͳĬÈϱàÂ룬Õâ»áµ¼ÖÂÈí¼þÔËÐнá¹ûµÃ²»È·¶¨¡£±ÈÈç¶ÔÓÚ"ÖÐÎÄ"£¬µ±ÏµÍ³Ä¬ÈϱàÂëΪ"gb2312"ʱ£¬½á¹ûÊÇ"%4e%2d%65%87"£¬¶øĬÈϱàÂëΪ"UTF-8"£¬½á¹ûÈ´ÊÇ"%e4%b8%ad%e6%96%87"£¬ºóÐø³ÌÐò½«ÄÑÒÔ´¦Àí¡£ÁíÍ⣬Õâ¶ù˵µÄϵͳĬÈϱàÂëÊÇÓÉÔËÐÐtomcatʱµÄ»·¾³±äÁ¿LC_ALLºÍLANGµÈ¾ö¶¨µÄ£¬Ôø¾­³öÏÖ¹ýtomcatÖØÆôºó¾Í³öÏÖÂÒÂëµÄÎÊÌ⣬×îºó²ÅÓôÃƵķ¢ÏÖÊÇÒòΪÐÞ¸ÄÐÞ¸ÄÁËÕâÁ½¸ö»·¾³±äÁ¿¡£

½¨Òéͳһָ¶¨Îª"UTF-8"±àÂ룬¿ÉÄÜÐèÒªÐÞ¸ÄÏàÓ¦µÄ³ÌÐò¡£

5.4. Ò»¸ö½â¾ö·½°¸

ÉÏÃæ˵Æð¹ý£¬ÒòΪä¯ÀÀÆ÷ÉèÖõIJ»Í¬£¬¶ÔÓÚͬһ¸öÁ´½Ó£¬web serverÊÕµ½µÄÊDz»Í¬ÄÚÈÝ£¬¶øÈí¼þϵͳÓÐÎÞ·¨ÖªµÀÕâÖмäµÄÇø±ð£¬ËùÒÔÕâһЭÒéÄ¿Ç°»¹´æÔÚȱÏÝ¡£

Õë¶Ô¾ßÌåÎÊÌ⣬²»Ó¦¸Ã½ÄÐÒÈÏΪËùÓпͻ§µÄIEÉèÖö¼ÊÇUTF-8ÓÐЧµÄ£¬Ò²²»Ó¦¸Ã´Ö±©µÄ½¨ÒéÓû§ÐÞ¸ÄIEÉèÖã¬ÒªÖªµÀ£¬Óû§²»¿ÉÄÜÈ¥¼Çסÿһ¸öweb serverµÄÉèÖá£ËùÒÔ£¬½ÓÏÂÀ´µÄ½â¾ö°ì·¨¾ÍÖ»ÄÜÊÇÈÃ×Ô¼ºµÄ³ÌÐò¶àÒ»µãÖÇÄÜ£º¸ù¾ÝÄÚÈÝÀ´·ÖÎö±àÂëÊÇ·ñUTF-8¡£

±È½ÏÐÒÔ˵ÄÊÇUTF-8±àÂëÏ൱ÓйæÂÉ£¬ËùÒÔ¿ÉÒÔͨ¹ý·ÖÎö´«Êä¹ýÀ´µÄÁ´½ÓÄÚÈÝ£¬À´ÅжÏÊÇ·ñÊÇÕýÈ·µÄUTF-8×Ö·û£¬Èç¹ûÊÇ£¬ÔòÒÔUTF-8´¦ÀíÖ®£¬Èç¹û²»ÊÇ£¬ÔòʹÓÿͻ§Ä¬ÈϱàÂ루±ÈÈç"GBK"£©£¬ÏÂÃæÊÇÒ»¸öÅжÏÊÇ·ñUTF-8µÄÀý×Ó£¬Èç¹ûÄãÁ˽âÏàÓ¦¹æÂÉ£¬¾ÍÈÝÒ×Àí½â¡£

public static boolean isValidUtf8(byte[] b,int aMaxCount){

int lLen=b.length,lCharCount=0;

for(int i=0;i
byte lByte=b[i++];//to fast operation, ++ now, ready for the following for(;;)

if(lByte>=0) continue;//>=0 is normal ascii

if(lByte<(byte)0xc0 || lByte>(byte)0xfd) return false;

int lCount=lByte>(byte)0xfc?5:lByte>(byte)0xf8?4

:lByte>(byte)0xf0?3:lByte>(byte)0xe0?2:1;

if(i+lCount>lLen) return false;

for(int j=0;j=(byte)0xc0) return false;

}

return true;

}

ÏàÓ¦µØ£¬Ò»¸öʹÓÃÉÏÊö·½·¨µÄÀý×ÓÈçÏ£º

public static String getUrlParam(String aStr,String aDefaultCharset)

throws UnsupportedEncodingException{

if(aStr==null) return null;

byte[] lBytes=aStr.getBytes("ISO-8859-1");

return new String(lBytes,StringUtil.isValidUtf8(lBytes)?"utf8":aDefaultCharset);

}

²»¹ý£¬¸Ã·½·¨Ò²´æÔÚȱÏÝ£¬ÈçÏÂÁ½·½Ã棺

l ûÓаüÀ¨¶ÔÓû§Ä¬ÈϱàÂëµÄʶ±ð£¬Õâ¿ÉÒÔ¸ù¾ÝÇëÇóÐÅÏ¢µÄÓïÑÔÀ´Åжϣ¬µ«²»Ò»¶¨ÕýÈ·£¬ÒòΪÎÒÃÇÓÐʱºòÒ²»áÊäÈëһЩº«ÎÄ£¬»òÕßÆäËûÎÄ×Ö¡£

l ¿ÉÄÜ»á´íÎóÅжÏUTF-8×Ö·û£¬Ò»¸öÀý×ÓÊÇ"ѧϰ"Á½¸ö×Ö£¬ÆäGBK±àÂëÊÇ" \xd1\xa7\xcf\xb0"£¬Èç¹ûʹÓÃÉÏÊöisValidUtf8·½·¨Åжϣ¬½«·µ»Øtrue¡£¿ÉÒÔ¿¼ÂÇʹÓøüÑϸñµÄÅжϷ½·¨£¬²»¹ý¹À¼ÆЧ¹û²»´ó¡£

ÓÐÒ»¸öÀý×Ó¿ÉÒÔÖ¤Ã÷googleÒ²Óöµ½ÁËÉÏÊöÎÊÌ⣬¶øÇÒÒ²²ÉÓÃÁ˺ÍÉÏÊöÏàËƵĴ¦Àí·½·¨£¬±ÈÈ磬Èç¹ûÔÚµØÖ·À¸ÖÐÊäÈë"http://www.google.com/search?hl=zh-CN&newwindow=1&q=ѧϰ"£¬google½«ÎÞ·¨Õýȷʶ±ð£¬¶øÆäËûºº×ÖÒ»°ãÄܹ»Õý³£Ê¶±ð¡£

×îºó£¬Ó¦¸Ã²¹³ä˵Ã÷һϣ¬Èç¹û²»Ê¹ÓÃrewrite¹æÔò£¬»òÕßͨ¹ý±íµ¥Ìá½»Êý¾Ý£¬Æäʵ²¢²»Ò»¶¨»áÓöµ½ÉÏÊöÎÊÌ⣬ÒòΪÕâʱ¿ÉÒÔÔÚÌá½»Êý¾Ýʱָ¶¨Ï£ÍûµÄ±àÂë¡£ÁíÍ⣬ÖÐÎÄÎļþÃûȷʵ»á´øÀ´ÎÊÌ⣬Ӧ¸Ã½÷É÷ʹÓá£

6. ÆäËü

ÏÂÃæÃèÊöһЩºÍ±àÂëÓйصÄÆäËûÎÊÌâ¡£

6.1. SecureCRT

³ýÁËä¯ÀÀÆ÷ºÍ¿ØÖÆ̨Óë±àÂëÓйØÍ⣬һЩ¿Í»§¶ËÒ²ºÜÓйØϵ¡£±ÈÈçÔÚʹÓÃSecureCRTÁ¬½Ólinuxʱ£¬Ó¦¸ÃÈÃSecureCRTµÄÏÔʾ±àÂ루²»Í¬µÄsession£¬¿ÉÒÔÓв»Í¬µÄ±àÂëÉèÖ㩺ÍlinuxµÄ±àÂë»·¾³±äÁ¿±£³ÖÒ»Ö¡£·ñÔò¿´µ½µÄһЩ°ïÖúÐÅÏ¢£¬¾Í¿ÉÄÜÊÇÂÒÂë¡£

ÁíÍ⣬mysqlÓÐ×Ô¼ºµÄ±àÂëÉèÖã¬Ò²Ó¦¸Ã±£³ÖºÍSecureCRTµÄÏÔʾ±àÂëÒ»Ö¡£·ñÔòͨ¹ýSecureCRTÖ´ÐÐsqlÓï¾äµÄʱºò£¬¿ÉÄÜÎÞ·¨´¦ÀíÖÐÎÄ×Ö·û£¬²éѯ½á¹ûÒ²»á³öÏÖÂÒÂë¡£

¶ÔÓÚUtf-8Îļþ£¬ºÜ¶à±à¼­Æ÷£¨±ÈÈç¼Çʱ¾£©»áÔÚÎļþ¿ªÍ·Ôö¼ÓÈý¸ö²»¿É¼ûµÄ±êÖ¾×Ö½Ú£¬Èç¹û×÷ΪmysqlµÄÊäÈëÎļþ£¬Ôò±ØÐëҪȥµôÕâÈý¸ö×Ö·û¡££¨ÓÃlinuxµÄvi±£´æ¿ÉÒÔÈ¥µôÕâÈý¸ö×Ö·û£©¡£Ò»¸öÓÐȤµÄÏÖÏóÊÇ£¬ÔÚÖÐÎÄwindowsÏ£¬´´½¨Ò»¸öÐÂtxtÎļþ£¬ÓüÇʱ¾´ò¿ª£¬ÊäÈë"Á¬Í¨"Á½¸ö×Ö£¬±£´æ£¬ÔÙ´ò¿ª£¬Äã»á·¢ÏÖÁ½¸ö×ÖûÁË£¬Ö»ÁôÏÂÒ»¸öСºÚµã¡£

6.2. ¹ýÂËÆ÷

Èç¹ûÐèҪͳһÉèÖñàÂ룬Ôòͨ¹ýfilter½øÐÐÉèÖÃÊǸö²»´íµÄÑ¡Ôñ¡£ÔÚfilter classÖУ¬¿ÉÒÔͳһΪÐèÒªµÄÇëÇó»òÕß»ØÓ¦ÉèÖñàÂë¡£²Î¼ÓÉÏÊösetCharacterEncoding()¡£Õâ¸öÀàapacheÒѾ­¸ø³öÁË¿ÉÒÔÖ±½ÓʹÓõÄÀý×ÓSetCharacterEncodingFilter¡£

6.3. POSTºÍGET

ºÜÃ÷ÏÔ£¬ÒÔPOSTÌá½»ÐÅϢʱ£¬URLÓиüºÃµÄ¿É¶ÁÐÔ£¬¶øÇÒ¿ÉÒÔ·½±ãµÄʹÓÃsetCharacterEncoding()À´´¦Àí×Ö·û¼¯ÎÊÌâ¡£µ«GET·½·¨ÐγɵÄURLÄܹ»¸üÈÝÒ×±í´ïÍøÒ³µÄʵ¼ÊÄÚÈÝ£¬Ò²Äܹ»ÓÃÓÚÊղء£

´ÓͳһµÄ½Ç¶È¿¼ÂÇÎÊÌ⣬½¨Òé²ÉÓÃGET·½·¨£¬ÕâÒªÇóÔÚ³ÌÐòÖлñµÃ²ÎÊýÊǽøÐÐÌØÊâ´¦Àí£¬¶øÎÞ·¨Ê¹ÓÃsetCharacterEncoding()µÄ±ãÀû£¬Èç¹û²»¿¼ÂÇrewrite£¬¾Í²»´æÔÚIEµÄUTF-8ÎÊÌ⣬¿ÉÒÔ¿¼ÂÇͨ¹ýÉèÖÃURIEncodingÀ´·½±ã»ñÈ¡URLÖеIJÎÊý¡£

6.4. ¼ò·±Ìå±àÂëת»»

GBKͬʱ°üº¬¼òÌåºÍ·±Ìå±àÂ룬Ҳ¾ÍÊÇ˵ͬһ¸ö×Ö£¬ÓÉÓÚ±àÂ벻ͬ£¬ÔÚGBK±àÂëÏÂÊôÓÚÁ½¸ö×Ö¡£ÓÐʱºò£¬ÎªÁËÕýÈ·È¡µÃÍêÕûµÄ½á¹û£¬Ó¦¸Ã½«·±ÌåºÍ¼òÌå½øÐÐͳһ¡£¿ÉÒÔ¿¼Âǽ«UTF¡¢GBKÖеÄËùÓз±Ìå×Ö£¬×ª»»ÎªÏàÓ¦µÄ¼òÌå×Ö£¬BIG5±àÂëµÄÊý¾Ý£¬Ò²Ó¦¸Ãת»¯³ÉÏàÓ¦µÄ¼òÌå×Ö¡£µ±È»£¬ÈÔ¾ÉÒÔUTF±àÂë´æ´¢¡£

ÀýÈ磬¶ÔÓÚ"ÓïÑÔ ?ÑÔ"£¬ÓÃUTF±íʾΪ"\xE8\xAF\xAD\xE8\xA8\x80 \xE8\xAA\x9E\xE8\xA8\x80"£¬½øÐмò·±Ìå±àÂëת»»ºóÓ¦¸ÃÊÇÁ½¸öÏàͬµÄ "\xE8\xAF\xAD\xE8\xA8\x80>"¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ