java ±àÂë UTF-8¡¢ISO-8859-1¡¢GBK ¡¾×ª¡¿
JavaÖ§³ÖUTF-8¡¢ISO-8859-1¡¢GBKµÈ¸÷ÖÖ×ÖÌå±àÂ룬¿É±ÊÕß·¢ÏÖJavaÖÐ×ÖÌå±àÂëµÄÎÊÌâÈÔÄѵ¹Á˲»ÉÙ³ÌÐòÔ±£¬ÍøÉÏËäȻҲÓв»ÉÙ¹ØÓÚÔÚJavaÖÐÈçºÎÕýÈ·ÏÔʾÖÐÎĵÄÎÄÕ£¬µ«¶¼²»¹»È«Ã棬±ÊÕßÌØÒâ×ܽáÈçÏ¡£
Ó° ÏìJavaÖÐ×ÖÌå±àÂëÕýÈ·ÏÔʾµÄÓм¸¸öÒòËØ: 1)Êý¾Ý¿âµÄÁ¬½Ó·½Ê½; 2)ÍøÒ³ÖÐʹÓõÄ×ÖÌå±àÂë; 3)Êý¾Ý¿âÀï´æ·ÅÊý¾ÝµÄ×ÖÌå±àÂë; 4)JavaµÄȱʡ×ÖÌå±àÂë¡£Èç¹ûÔÚ±à³ÌÖÐÓöµ½²»ÄÜÕýÈ·ÏÔʾÖÐÎÄʱ£¬ÒªÏÈŪÇå³þÒÔÉϼ¸ÏîËùʹÓõÄ×ÖÌå±àÂ룬ÔÙ·ÖÎöÕÒ³öÔÒò£¬¼´¿É½â¾öÎÊÌâ¡£
ÖÚ ËùÖÜÖª£¬JSPÊÇJavaµÄÒ»ÖÖ£¬ºÍÍøÒ³Óйأ¬¶øÍøÒ³Ò²ÓÐ×Ô¼ºµÄÖÐÎıàÂëϵͳ£¬ËùÒÔJSP´¦ÀíÖÐÎÄÒª±È´¿JavaµÄÀàÎļþ¸üΪÂé·³¡£±¾ÎĵIJâÊÔÊý¾Ý¿âÊÇ MySQL3.2£¬Êý¾Ý¿âÁ¬½ÓÇý¶¯ÊÇÓÃorg.gjt.mm.mysql.Driver£¬ÕâÀïÖ÷ÒªÌÖÂÛUTF-8ºÍGBKµÄÏÔʾ( GB2312ÊÇGBKµÄÒ»¸ö×Ó¼¯£¬JavaÖпÉÒÔʹÓÃGBKÀ´´úÌæGBϵÁÐ)¡£ÎÒÃÇÏÈÀ´Ñо¿JSPÖÐ×ÖÌå±àÂëÎÊÌ⣬ÏÂÃæµÚÒ»µ½µÚÁùµãÊÇÕë¶ÔJSPµÄ(ÒòΪ ´ÓÊý¾Ý¿âÀï¶Á³öÖÐÎÄÊý¾ÝÓëдÈëÖÐÎÄÊý¾ÝÓÐËùÇø±ð£¬ÔÛÃÇ·Ö±ð˵Ã÷£¬Ç°ÈýµãÊÇ´Ó¶ÁÈ¡Êý¾Ý¿âµ½ÏÔʾÔÚÍøÒ³£¬ºóÈýµãÊÇ´ÓÍøÒ³ÊäÈëÊý¾Ýµ½´æÈëÊý¾Ý¿â)£¬µÚÆßµ½µÚ¾Åµã Õë¶Ô´¿JavaµÄÀàÎļþ¡£ÒÔÏÂrs±íʾResultSetµÄÒ»¸öʵÀý£¬ÊÇÖ´ÐÐSelectÓï¾äÖ®ºó²úÉúµÄÊý¾Ý¼¯¡£
Ò»¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃUTF-8
ÔÚÁ¬½ÓÊý¾Ý¿âµÄÇý¶¯ºóÃæ¼ÓÉÏÕâ¾ä²ÎÊýuseUnicode=true&characterEncoding=
UTF-8£¬ÀýÈçjdbc:mysql://localhost/DBVF?autoReconnect=true&useUnicode=
true&characterEncoding=UTF-
8£¬´ÓÊý¾Ý¿âÀï¶Á³öÖÐÎÄÏÔʾÔÚʹÓÃGBKµÄJSPµÄÍøÒ³ÀÈç¹ûÊý¾Ý¿âÀï´æ·ÅµÄ×ÖÌå±àÂëÊÇUTF-8£¬ÔÚJSPÖÐʹÓà str=new
String(rs.getBytes(1),"UTF-8")»òÕßstr=rs.getString(1)£¬¿ÉÒÔÕýÈ·ÏÔʾÖÐÎÄ¡£Èç¹ûÊý¾Ý¿âÀï´æ·ÅµÄÊÇ
GBKÊý¾Ý£¬ÄÇôJSPÖÐҲҪʹÓÃstr=new
String(rs.getBytes(1),"GBK")À´ÏÔʾÕýÈ·µÄÖÐÎÄ¡£ÖµµÃ×¢ÒâµÄÊÇÈç¹ûÒ³ÃæʹÓÃUTF-8£¬Êý¾Ý¿âÀï´æ·ÅµÄÊÇUTF-8£¬Ò²¿ÉÒÔ
ÓÃstr=new
String(rs.getBytes(1),"GBK")ÕýÈ·ÏÔʾÖÐÎÄ¡£Èç¹ûÍøÒ³ÊÇUTF-8£¬¶øÊý¾Ý¿âÀï´æ·ÅµÄÊÇGBK£¬ÎÞ·¨Ö±½ÓÏÔʾÖÐÎÄ£¬ÐèÒª2²½
ת»», str=new String(rs.getBytes(1),"GBK"); ÔÙstr=new
String(str.getBytes("UTF-8"),"GBK")£¬²Å¿ÉÒÔÕýÈ·ÏÔʾÖÐÎÄ¡£
¶þ¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃGBK
ÔÚÁ¬½ÓÊý¾Ý¿âµÄÇý¶¯ºóÃæ¼ÓÉÏÕâ¾ä²ÎÊýuseUnicode=true&characterEncoding=
GBK,ÀýÈçjdbc:mysql://localhost/DBVF?autoReconnect=true&UseUnicode=true&
characterEncoding=GBK£¬
´ÓÊý¾Ý¿âÀï¶Á³öÖÐÎÄ£¬ÏÔʾÔÚʹÓÃGBKµÄJSPµÄÍøÒ³ÀÈç¹ûÊý¾Ý¿âÀï´æ·ÅµÄ×ÖÌå±àÂëÊÇ UTF-8£¬ÔÚJSPÖÐÒ»¶¨ÒªÊ¹Óà str=new
String(rs.getBytes(1),"UTF-8")£¬²ÅÕýÈ·ÏÔʾÖÐÎÄ¡£Èç¹ûÊý¾Ý¿âÀï´æ·ÅµÄÊÇGBKÊý¾Ý£¬ÄÇôJSPÖÐҲҪʹÓÃstr=new
String(rs.getBytes(1),"GBK")
»òÕßÖ±½ÓʹÓÃstr=rs.getString(1)£¬¼´¿ÉÏÔʾÕýÈ·µÄÖÐÎÄ¡£Èç¹ûÍøÒ³ÊÇUTF-8£¬¶øÊý¾Ý¿âÀï´æ·ÅµÄÊÇGBK£¬Ö»ÄÜÓÃstr=new
String(rs.getString(1).getBytes("UTF-8"),"GBK")µÄ·½·¨À´ÏÔʾÖÐÎÄ;
Èç¹ûÍøÒ³ÊÇUTF-8£¬¶øÊý¾Ý¿âÀï´æ·ÅµÄÊÇUTF-8£¬¿ÉÓÃstr=new String(rs.getBytes(1),"GBK")
»òÕßrs.getString(1)·½·¨À´ÏÔʾÖÐÎÄ¡£
Èý¡¢Ê¹ÓÃȱʡÊý¾Ý¿âÁ¬½Ó·½Ê½
Á¬½ÓÊý¾Ý¿âµÄÇý¶¯ºóÃæûÓÐÕâ¾ä²ÎÊý
useUnicode=&characterEncoding=£¬ÀýÈç
jdbc:mysql://localhost/DBVF?autoReconnect=true£¬Ã»ÓвÎÊýuseUnicode=true&
characterEncoding£¬±íʾʹÓÃĬÈϵÄISO-8895-1±àÂë¡£
1.
´ÓÊý¾Ý¿âÀï¶Á³öÖÐÎÄ£¬ÏÔʾÔÚGBKµÄÍøÒ³Àï¡£Èç¹ûÊý¾Ý¿âÀï´æ·ÅµÄ×ÖÌå±àÂëÊÇUTF-8£¬ÔÚJSPÍøÒ³ÖÐÒ»¶¨ÒªÊ¹ÓÃÓï¾ä str=new
String(rs.getBytes(1),"UTF-8") »òÕßstr= new
String(rs.getString(1).getBytes("ISO-8859-1"),"UTF-8")£¬²Å¿ÉÕýÈ·ÏÔʾÖÐÎÄ¡£Èç¹ûÊý¾Ý¿âÀï´æ·Å
µÄÊÇGBKÊý¾Ý£¬ÄÇôJSPÖÐҲҪʹÓÃstr=new String(rs.getBytes(1),"GBK")»òstr=new
String(rs.getString(1).getBytes("ISO-8859-1"),"GBK") ÏÔʾÕýÈ·µÄÖÐÎÄ¡£
2.
Èç¹ûÍøÒ³ÊÇUTF-8,²»ÄÜÖ±½ÓÕýÈ·ÏÔʾGBK£¬ÐèÒª2²½×ª»»£¬str=new
String(rs.getBytes(1),"GBK")£¬ÔÙstr=new
String(str.getBytes("UTF-8"),"GBK") ²Å¿ÉÒÔÕýÈ·ÏÔʾÖÐÎÄ¡£Èç¹ûÊý¾Ý¿âÀï´æµÄÊÇUTF-8£¬Ö±½ÓÓÃstr=new
String(rs.getBytes(1),"GBK")»òÕßstr=new
String(rs.getString(1).getBytes("ISO-8859-1"),"GBK")¾Í¿ÉÒÔÏÔʾÖÐÎÄÁË¡£
ÒÔÉÏÊǶÁÈ¡Êý¾Ý¿âÀïÖÐÎÄÕýÈ·ÏÔʾÔÚÍøÒ³ÉÏ£¬ÏÂÃæÈýµãÊÇÈçºÎÕýÈ·´æÈëÊý¾Ý¿â¡£
ËÄ¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃUTF-8±àÂë
JSP
ÖÐÒª°ÑÍøÒ³ÊäÈëµÄÖÐÎÄ´æÈëÊý¾Ý¿â£¬Í¨³£ÓÐÒ»¸öÌá½»(Submit)µÄ¹ý³Ì£¬ÊÇÓÃ
str=request.getParameter("username")£¬È»ºóÖ´ÐÐupdate»òÕßinsertÓï¾äÀ´´æÈëÊý¾Ý¿â¡£ÈçºÎ¸³Öµ¸østr
ºÜÖØÒª£¬¶øÇÒÕâÀïÖÐÎÄÊäÈëÓëÍøÒ³ËùʹÓõÄ×ÖÌå±àÂëÓйء£
1¡¢ ÍøҳʹÓÃUTF-8£¬Ê¹ÓÃstr= new
String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8")»ò
Õßstr= new
String(request.getParameter("username").getBytes(),"UTF-8")£¬¶¼¿ÉÒÔʹµÃ´æµ½Êý¾Ý¿âÀïµÄ
Êý¾ÝÊÇUTF-8±àÂë¡£
2. ÍøҳʹÓÃGBK£¬Ê¹ÓÃstr= new String(request.getParameter("username").getBytes(),"GBK")£¬ÄÇô´æÈëÊý¾Ý¿âµÄÊÇUTF-8±àÂë¡£
3. ÖµµÃ×¢ÒâµÄÊÇʹÓÃUTF-8µÄÊý¾Ý¿âÁ¬½Ó·½Ê½²»ÄÜ´æµÃGBK¡£
Îå¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃGBK±àÂë
1.
ÊäÈëʹÓÃGBKÍøÒ³£¬´æµ½Êý¾Ý¿âÀïÊÇGBKµÄ·½·¨: str= new
String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK")
»òÕßstr= new String(request.getParameter("username").getBytes(),"GBK")¡£
2.
ÍøҳʹÓÃGBK£¬Ïë´æÈëUTF-8µ½Êý¾Ý¿âÀҪ·Ö2²½: ÏÈstr=new
String(request.getParameter("username").getBytes(),"GBK")£¬ÔÙstr=new
String(str.getBytes("UTF-8"),"GBK")¼´¿É¡£
3. ÍøҳʹÓÃUTF-8£¬¶øÇÒʹÓÃstr= new
String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK")
»òÕßstr= new
String(request.getParameter("username").getBytes(),"UTF-8")£¬ÄÇô´æµ½Êý¾Ý¿âÀïµÄÊý¾ÝÊÇ
UTF-8±àÂë¡£
4. ÍøҳʹÓÃUTF-8£¬¶øÇÒʹÓÃstr= new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8")£¬ÄÇô´æµ½Êý¾Ý¿âÀïµÄÊý¾ÝÊÇGBK±àÂë¡£
Áù¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃȱʡ£¬¼´²»Ê¹ÓòÎÊýuseUnicodeºÍcharacterEncoding
1.
ÍøҳʹÓÃGBK£¬Èç¹ûʹÓÃstr= request.getParameter("username")»òÕßstr= new
String(request.getParameter("username").getBytes())£¬ÄÇôÔÚÊý¾Ý¿âÀïµÄÊý¾ÝÊÇGBKÂë¡£ÍøҳʹÓÃ
UTF-8 ºÍʹÓÃstr= request.getParameter("username")£¬Ôò´æÈëÊý¾Ý¿âÊÇUTF-8±àÂë¡£
2.
Èç¹ûʹÓÃstr= new
String(request.getParameter("username").getBytes("ISO-8859-1"))£¬ÄÇô¸ù¾ÝÍøÒ³Ìṩ
µÄ×ÖÌå±àÂë¶ø´æµ½Êý¾Ý¿âÀ±ÈÈçÊÇUTF-8µÄÍøÒ³£¬ÄÇô´æµ½Êý¾Ý¿âÖоÍÊÇUTF-8±àÂ룬Èç¹ûʹÓÃGBKÍøÒ³£¬ÄÇô´æµ½Êý¾Ý¿âÀïµÄ×Ö¾ÍÊÇGBK±àÂë¡£
3.
Èç¹ûʹÓÃstr= new
String(request.getParameter("username").getBytes("UTF-8"),"UTF-8")ÕâÒ»ÖÖ×éºÏÄÜ
´æµ½ÕýÈ·µÄÊý¾ÝÍ⣬ÆäËû´æµ½Êý¾Ý¿âÀïµÄÊý¾ÝÔò¶¼ÊÇÂÒÂë»òÕß´íÎóÂë¡£ÔÚÕâ¸öUTF-8×éºÏµÄÌØÀýÖУ¬ÍøҳʹÓõÄÊÇGBK£¬Ôò´æ·Åµ½Êý¾Ý¿âÀï¾ÍÊÇGBK£¬ÍøÒ³
ʹÓÃUTF-8£¬ÄÇô´æµ½Êý¾Ý¿âÀïµÄ¾ÍÊÇUTF-8¡£
4. ÍøÒ³ÊÇGBKµÄÒª´æµÃUTF-8£¬Ò»¶¨ÐèÒª2²½: company=new
String(request.getParameter("company").getBytes(),"GBK")ºÍcompany=new
String(company.getBytes("UTF-8"))¡£
5. ÍøÒ³ÊÇUTF-8µÄ£¬²»ÄÜ´æµÃGBKÔÚÊý¾Ý¿âÀһ¾ä»°£¬¸Ä±äÊý¾Ý¿âÁ¬½Ó·½Ê½²»ÄÜ´æµÃGBKÂë¡£
ÒÔÉÏËùÓеĶ¼ÊÇ»ùÓÚJSPÍøÒ³ºÍÊý¾Ý¿â½»»»Êý¾Ý£¬ÏÂÃæÌÖÂÛһϴ¿JAVA±à³ÌϵÄ×ÖÌå±àÂëת»»¡£
Æß¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃUTF-8±àÂë
1. Êý¾Ý¿âÀïµÄÖÐÎÄÊÇUTF-8£¬¿ÉÒÔת»»ÎªGBK,µ«²»ÄÜ°ÑGBK´æÈëÊý¾Ý¿â¡£
2. Êý¾Ý¿âÊÇGBK£¬Èç¹ûת»»ÎªUTF-8£¬Ê¹ÓÃcontent=new String(rs.getBytes(2),"GBK")Ö±½Ó½«content´æÈëÊý¾Ý¿â¾Í¿ÉΪUTF-8¡£
°Ë¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃGBK±àÂë
1.
Êý¾Ý¿âÀïµÄÖÐÎÄÊÇUTF-8£¬Èç¹ûת»»ÎªGBK£¬Ê¹ÓÃcontent= new
String(rs.getString(2).getBytes(),"UTF-8"),ÔÙÖ±½ÓʹÓÃupdate»òÕßinsertÓï¾ä²åÈëµ½Êý¾Ý¿â£¬¼´
´æµÃGBK¡£Èç¹ûʹÓÃcontent= new
String(rs.getString(2).getBytes(),"GBK")»òÕßcontent= new
String(rs.getString(2).getBytes())£¬ÔÙ´æÈëÊý¾Ý¿â¼´´æµÃ»¹ÊÇUTF-8±àÂë¡£
2.
Êý¾Ý¿âÀïµÄÖÐÎÄÊÇGBK£¬Èç¹ûת»»ÎªUTF-8£¬Ê¹ÓÃcontent= new
String(rs.getString(2).getBytes("UTF-8"))»òÕßcontent= new
String(rs.getString(2).getBytes("UTF-8"),"GBK")£¬ÔÙÖ±½ÓʹÓÃupdate»òÕßinsertÓï¾ä²åÈëµ½
Êý¾Ý¿â,¼´´æµÃUTF-8¡£
3. Èç¹ûij¸öStringÊÇGBK£¬Òª×ª»»ÎªUTF-8£¬Ò²ÊÇʹÓÃcontent= new
String(GBKstr.getBytes("UTF-8"))»òÕßcontent= new
String(GBKstr.getBytes("UTF-8"),"GBK"); Èç¹ûij¸öStringÊÇUTF-8£¬Òª×ª»»ÎªGBK£¬Ó¦¸ÃʹÓÃnew
String(UTFstr.getBytes("GBK"),"UTF-8")¡£
¾Å¡¢Êý¾Ý¿âÁ¬½Ó·½Ê½Ê¹ÓÃȱʡ,¼´²»¸ú²ÎÊý
1. str2=new String(GBKstr.getBytes("UTF-8"),"ISO-8859-1")£¬¿ÉÒÔ½«Êý¾Ý¿âÀïµÄGBK±àÂëת»»ÎªUTF-8¡£
2. ¶ÁÈ¡UTF-8È»ºó´æÈëUTF-8£¬ÔòÓÃstr1=new String(UTFstr.getBytes(),"ISO-8859-1")»òÕßstr1=new String(UTFstr.getBytes("GBK"),"ISO-8859-1")¡£
3. ²»ÄÜʵÏÖÊý¾Ý¿âÀïµÄUTF-8ת»»ÎªGBK¡£
Èç¹û²ÉÓÃUTF-8µÄÊý¾Ý¿âÁ¬½Ó·½Ê½»òÕßȱʡÊý¾ÝÁ¬½Ó·½Ê½£¬ÄÇôÎÞ·¨½«UTF-8תΪGBK£»¶øGBKµÄÊý¾Ý¿âÁ¬½Ó·½Ê½¿ÉÒÔʵÏÖUTF-8ºÍGBKµÄÏ໥ת»»¡£½¨Òé´ó¼Ò²ÉÓÃGBKµÄÊý¾ÝÁ¬½Ó·½Ê½¡£
ÍƼöÐÅÏ¢
- ÔÝÎ޼Ǽ
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- java urlÖØд¼¼Êõ(UrlReWriter)
- [ת]JavaµÄÊý×飨Array£©¡¢Vector¡¢ArrayLis....
- jsp»·¾³ÏÂajaxÂÒÂëÎÊÌâµÄ½â¾ö
- JSP¼¼Êõ Ö¸Áî ÂÒÂëÎÊÌâ ÔËÐÐÔÀíºÍ¾Å´óÒþʽ¶ÔÏó web¿ª·¢µÄË....
- JAVA ˮӡ ͼƬ ÎÄ×Ö
- java¶þ½øÖÆ£¬ÔÂë¡¢·´Âë¡¢²¹Â롢λÔËËã
- Java WebÓ¦ÓÃÆô¶¯¼ä¸ôÖ´ÐеijÌÐò
- jspÉú³ÉxmlÎļþʾÀý
- javaÖÐÀà±äÁ¿¡¢Àà·½·¨¡¢ÊµÀý±äÁ¿¡¢ÊµÀý·½·¨
- JSPÒ³ÃæµÄÎåÖÖÌøת·½·¨
ÆÀÂÛ