当前位置: 代码迷 >> JavaScript >> Java中准确解码用Javascript:escape()编码的中文字符
  详细解决方案

Java中准确解码用Javascript:escape()编码的中文字符

热度:668   发布时间:2013-02-25 10:23:36.0
Java中正确解码用Javascript:escape()编码的中文字符
 做页面开发时使用了Javascript的escape()函数进行URL编码,如果url中有中文,结果在JSP或Servlet中用request.getParameter()时取不到参数,只返回一个空值。经过Baidu的搜索,结合我自己的工作实际,再加上网友的一些帮助,终于找到了问题所在及解决的办法。

        本来Javascript中的escape()是将中文按ISO-8859-1字符集进行URL编码的,那样通过 request.getParameter()是能直接获取到请求参数的,但后来的Javascript将escape()换成了Unicode字符集编 码,如此一来,在JSP和Servlet中就没法直接拿到请求参数了,而且此时的request获得的参数值都是空值,想了好久,也测试了不少,但是还是百思不得其解。 最后借助于网络,终于解决了此问题,我露出了灿烂的笑容?

        解决办法:

        1、首先对中文字符进行两次escape()编码,如要传参数name,值为“黄修群”,则url的格式为....name=escape(escape("黄修群")),这样一来,在request.getParameter()就能取到编码后的参数了,但是参数去编码的形式:如下:

%u9EC4%u4FEE%u7FA4,

获得的页面参数为:获取request参数值-->%u9EC4%u4FEE%u7FA4

此时在使用java.net.URIDecoder.decode()方法,则出现解码错误,错误信息如下:

2010-5-19 15:28:03 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "u9"
 at java.net.URLDecoder.decode(URLDecoder.java:173)
 at com.mapgis.vfd.plugins.vfdwebserver.VFDWebServer.getPageParameterList(VFDWebServer.java:674)

 

可见是解码出现了问题了

也许有人问,那我在客户端非要只用一次escape方法编码,会出现什么样的结果?请看下面:

2010-5-19 14:59:38 org.apache.tomcat.util.http.Parameters processParameters
警告: Parameters: Character decoding failed. Parameter skipped.
java.io.CharConversionException: isHexDigit
 at org.apache.tomcat.util.buf.UDecoder.convert(UDecoder.java:88)
 at org.apache.tomcat.util.buf.UDecoder.convert(UDecoder.java:49)
 at org.apache.tomcat.util.http.Parameters.urlDecode(Parameters.java:412)
 at org.apache.tomcat.util.http.Parameters.processParameters(Parameters.java:394)

。。。。。

看到上面的提示信息,可以断定是解码出现问题了,而获得的请求对象去是空的,如下:

获取request参数值-->null

 

 

        2、由于取到的参数是  u9EC4%u4FEE%u7FA4格式的,没法用常规的URLDecoder.decode()来进行解码,还好,这世上的牛人够多,在网上直接找到了一个工具类,能实现 Javascript中escape()及unescape()式的编解码,源代码如下:

[java] view plaincopyprint?
  1. /** 
  2.  * JavaScript escape/unescape 编码的 Java 实现 
  3.  * author jackyz 
  4.  * keep this copyright info while using this method by free 
  5.  */  
  6. public class Escape {  
  7.     private final static String[] hex = {  
  8.         "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",  
  9.         "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",  
  10.         "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",  
  11.         "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",  
  12.         "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",  
  13.         "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",  
  14.         "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",  
  15.         "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",  
  16.         "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",  
  17.         "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",  
  18.         "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",  
  19.         "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",  
  20.         "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",  
  21.         "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",  
  22.         "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",  
  23.         "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"  
  24.     };  
  25.     private final static byte[] val = {  
  26.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  27.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  28.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  29.         0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  30.         0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  31.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  32.         0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  33.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  34.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  35.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  36.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  37.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  38.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  39.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  40.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,  
  41.         0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F  
  42.     };  
  43.     public static String escape(String s) {  
  44.         StringBuffer sbuf = new StringBuffer();  
  45.         int len = s.length();  
  46.         for (int i = 0; i < len; i++) {  
  47.             int ch = s.charAt(i);  
  48.             if (ch == ' ') {                        // space : map to '+'    
  49.                 sbuf.append('+');  
  50.             } else if ('A' <= ch && ch <= 'Z') {    // 'A'..'Z' : as it was   
  51.                 sbuf.append((char)ch);  
  52.             } else if ('a' <= ch && ch <= 'z') {    // 'a'..'z' : as it was   
  53.                 sbuf.append((char)ch);  
  54.             } else if ('0' <= ch && ch <= '9') {    // '0'..'9' : as it was   
  55.                 sbuf.append((char)ch);  
  56.             } else if (ch == '-' || ch == '_'       // unreserved : as it was   
  57.                 || ch == '.' || ch == '!'  
  58.                 || ch == '~' || ch == '*'  
  59.                 || ch == '/'' || ch == '('  
  60.                 || ch == ')') {  
  61.                 sbuf.append((char)ch);  
  62.             } else if (ch <= 0x007F) {              // other ASCII : map to %XX   
  63.                 sbuf.append('%');  
  64.                 sbuf.append(hex[ch]);  
  65.             } else {                                // unicode : map to %uXXXX   
  66.                 sbuf.append('%');  
  67.                 sbuf.append('u');  
  68.                 sbuf.append(hex[(ch >>> 8)]);  
  69.                 sbuf.append(hex[(0x00FF & ch)]);  
  70.             }  
  71.         }  
  72.         return sbuf.toString();  
  73.     }  
  74.     public static String unescape(String s) {  
  75.         StringBuffer sbuf = new StringBuffer();  
  76.         int i = 0;  
  77.         int len = s.length();  
  78.         while (i < len) {  
  79.             int ch = s.charAt(i);  
  80.             if (ch == '+') {                        // + : map to ' '    
  81.                 sbuf.append(' ');  
  82.             } else if ('A' <= ch && ch <= 'Z') {    // 'A'..'Z' : as it was   
  83.                 sbuf.append((char)ch);  
  84.             } else if ('a' <= ch && ch <= 'z') {    // 'a'..'z' : as it was   
  85.                 sbuf.append((char)ch);  
  86.             } else if ('0' <= ch && ch <= '9') {    // '0'..'9' : as it was   
  87.                 sbuf.append((char)ch);  
  88.             } else if (ch == '-' || ch == '_'       // unreserved : as it was   
  89.                 || ch == '.' || ch == '!'  
  90.                 || ch == '~' || ch == '*'  
  91.                 || ch == '/'' || ch == '('  
  92.                 || ch == ')') {  
  93.                 sbuf.append((char)ch);  
  94.             } else if (ch == '%') {  
  95.                 int cint = 0;  
  96.                 if ('u' != s.charAt(i+1)) {         // %XX : map to ascii(XX)   
  97.                     cint = (cint << 4) | val[s.charAt(i+1)];  
  98.                     cint = (cint << 4) | val[s.charAt(i+2)];  
  99.                     i+=2;  
  100.                 } else {                            // %uXXXX : map to unicode(XXXX)   
  101.                     cint = (cint << 4) | val[s.charAt(i+2)];  
  102.                     cint = (cint << 4) | val[s.charAt(i+3)];  
  103.                     cint = (cint << 4) | val[s.charAt(i+4)];  
  104.                     cint = (cint << 4) | val[s.charAt(i+5)];  
  105.                     i+=5;  
  106.                 }  
  107.                 sbuf.append((char)cint);  
  108.             }  
  109.             i++;  
  110.         }  
  111.         return sbuf.toString();  
  112.     }  
  113.     public static void main(String[] args) {  
  114.         String stest = "黄修群1234 abcd[]()<+>,.~//";  
  115.         System.out.println(stest);  
  116.         System.out.println(escape(stest));  
  117.         System.out.println(unescape(escape(stest)));  
  118.     }  
  119. }  

代码运行如下:

黄修群1234 abcd[]()<+>,.~/
%u9EC4%u4FEE%u7FA41234+abcd%5B%5D()%3C%2B%3E%2C.~%5C
黄修群1234 abcd[]()<+>,.~/
黄修群

有了这个工具类,直接用Escape.unescape()方法就能将获取到的参数解码成中文格式了。

paraValue= Escape.unescape(request.getParameter(name).toString().trim());

结果输出了:黄修群

幸福吧,哈哈!

  相关解决方案