`
032615
  • 浏览: 60992 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

在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中就没法直接拿到请求参数了,具体原因我也不知道。

        解决办法:

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

        2、由于取到的参数是  %25u4F60%25u597D  格式的,没法用常规的URLDecoder.decode()来进行解码,还好,这世上的牛人够多,在网上直接找到了一个工具类,能实现 Javascript中escape()及unescape()式的编解码,源代码如下:
**
 * JavaScript escape/unescape 编码的 Java 实现
 * author jackyz
 * keep this copyright info while using this method by free
 */
public class Escape {
    private final static String[] hex = {
        "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
        "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
        "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
        "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
        "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
        "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
        "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
        "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
        "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
        "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
        "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
        "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
        "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
        "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
        "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
        "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"
    };
    private final static byte[] val = {
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F
    };
    public static String escape(String s) {
        StringBuffer sbuf = new StringBuffer();
        int len = s.length();
        for (int i = 0; i < len; i++) {
            int ch = s.charAt(i);
            if (ch == ' ') {                        // space : map to '+'
                sbuf.append('+');
            } else if ('A' <= ch && ch <= 'Z') {    // 'A'..'Z' : as it was
                sbuf.append((char)ch);
            } else if ('a' <= ch && ch <= 'z') {    // 'a'..'z' : as it was
                sbuf.append((char)ch);
            } else if ('0' <= ch && ch <= '9') {    // '0'..'9' : as it was
                sbuf.append((char)ch);
            } else if (ch == '-' || ch == '_'       // unreserved : as it was
                || ch == '.' || ch == '!'
                || ch == '~' || ch == '*'
                || ch == '\'' || ch == '('
                || ch == ')') {
                sbuf.append((char)ch);
            } else if (ch <= 0x007F) {              // other ASCII : map to %XX
                sbuf.append('%');
                sbuf.append(hex[ch]);
            } else {                                // unicode : map to %uXXXX
                sbuf.append('%');
                sbuf.append('u');
                sbuf.append(hex[(ch >>> 8)]);
                sbuf.append(hex[(0x00FF & ch)]);
            }
    &nbsp;   }
        return sbuf.toString();
    }
    public static String unescape(String s) {
        StringBuffer sbuf = new StringBuffer();
        int i = 0;
        int len = s.length();
        while (i < len) {
            int ch = s.charAt(i);
            if (ch == '+') {                        // + : map to ' '
                sbuf.append(' ');
            } else if ('A' <= ch && ch <= 'Z') {    // 'A'..'Z' : as it was
                sbuf.append((char)ch);
            } else if ('a' <= ch && ch <= 'z') {    // 'a'..'z' : as it was
                sbuf.append((char)ch);
            } else if ('0' <= ch && ch <= '9') {    // '0'..'9' : as it was
                sbuf.append((char)ch);
            } else if (ch == '-' || ch == '_'       // unreserved : as it was
                || ch == '.' || ch == '!'
                || ch == '~' || ch == '*'
                || ch == '\'' || ch == '('
                || ch == ')') {
                sbuf.append((char)ch);
            } else if (ch == '%') {
                int cint = 0;
                if ('u' != s.charAt(i+1)) {         // %XX : map to ascii(XX)
                    cint = (cint << 4) | val[s.charAt(i+1)];
                    cint = (cint << 4) | val[s.charAt(i+2)];
                    i+=2;
                } else {                            // %uXXXX : map to unicode(XXXX)
                    cint = (cint << 4) | val[s.charAt(i+2)];
                    cint = (cint << 4) | val[s.charAt(i+3)];
                    cint = (cint << 4) | val[s.charAt(i+4)];
                    cint = (cint << 4) | val[s.charAt(i+5)];
                    i+=5;
                }
                sbuf.append((char)cint);
            }
            i++;
        }
        return sbuf.toString();
    }
    public static void main(String[] args) {
        String stest = "中文1234 abcd[]()<+>,.~\\";
        System.out.println(stest);
        System.out.println(escape(stest));
        System.out.println(unescape(escape(stest)));
    }
}
有了这个工具类,直接用Escape.unescape()方法就能将获取到的参数解码成中文格式了。

分享到:
评论
3 楼 hupengfeijiayou 2009-09-17  
  其实不用那个工具类那么麻烦,在客户端对将要发送的中文字符进行两次EncodeURI("中文"),然后在服务端用URLDecoder.decode()对接受到的客户端参数进行解码就可以得到正确的中文字符。
这是一种通用的解决方案,希望楼主可以试验一下。
2 楼 lianda220 2009-09-03  
很好 很强大啊   呵呵 
1 楼 mtvwang 2009-08-19  
及时雨啊  , 解决了 我的问题啊
很好。

相关推荐

    js对中文字符串进行gb2312编码解码

    在JavaScript中进行GB2312编码和解码可以帮助我们正确地在不同环境间传输和展示中文字符。 首先,让我们理解GB2312编码的工作原理。GB2312编码是基于两个字节的,每个汉字或符号由两个连续的字节表示。对于一个GB...

    JavaScript、JavaWeb对汉字等的编码与解码处理

    在本资源中,我们将详细介绍JavaScript和JavaWeb对汉字编码的策略,包括escape、encodeURI、encodeURIComponent等编码函数的使用,以及对应的解码函数unescape、decodeURI、decodeURIComponent的使用。 一、escape...

    php 正确解码javascript中通过escape编码后的字符

    标题中的知识点主要涉及的是在PHP中如何正确地解码JavaScript的`escape`编码。JavaScript的`escape`函数用于对字符串进行编码,以便在URL、HTML或其他非文本上下文中使用。编码后的字符串会将某些特殊字符替换为...

    java和js相互base64编码解码

    在Java和JavaScript中,Base64编码和解码是两个重要的功能,用于处理这种编码格式。下面将详细阐述这两个编程语言中实现Base64编码和解码的方法。 首先,让我们关注Java。在Java 8及更高版本中,已经内置了`java....

    escape编码工具

    - **escape**:这指的是JavaScript中的`escape()`函数,它用于对字符串进行编码,特别是在发送数据到服务器或在URL中使用时。 - **javascript**:JavaScript是Web开发中常用的客户端脚本语言,与ASP一起,可能被这个...

    java实现js的escape和unescape

    通过上述讨论,我们不仅了解了如何在Java中实现JavaScript的`escape`和`unescape`功能,还深入了解了编码与解码的基本原理。这对于跨语言的数据处理和网络通信具有重要的实践意义。掌握这些技巧,能够帮助开发者更加...

    javascript 对url编码 解码

    JavaScript 对 URL 编码解码 JavaScript 中对 URL 编码和解码涉及到六个函数:escape、encodeURI、...理解和正确使用 JavaScript 中的编码和解码函数是非常重要的,以便在 web 开发中正确地传递和处理数据。

    javascript编码与解码

    `unescape()`可以解码由`escape()`函数编码的字符串,但这两个函数在现代JavaScript中已不推荐使用,取而代之的是更安全的`encodeURI()`、`encodeURIComponent()`、`decodeURI()`和`decodeURIComponent()`。...

    取Escape编码.rar

    Escape编码的核心是使用反斜杠(\)作为前缀,接着是字符的特定表示,比如在JavaScript中,非ASCII字符、特殊字符如换行符(\n)、制表符(\t)、双引号(\")等都会被转义。在其他编程语言中,如Python,可能会使用类似"\...

    java中文乱码之解决URL中文乱码问题的方法

    - **一次转码**:使用`encodeURI()` 对URL进行编码,然后在服务器端使用适当的解码方法,如Java中的`new String(name.getBytes("ISO-8859-1"), "UTF-8")`,确保从ISO-8859-1转回UTF-8。 - **二次转码**:先使用`...

    java版本的escape和unescape函数

    而在Java中,我们通常使用`java.net.URLDecoder.decode()`来解码URL编码的字符串,将`%`符号后面跟随的十六进制数字转换回相应的字符。 `EscapeUnescape.java`文件可能包含了演示`escape`和`unescape`功能的示例...

    url传值异常java.io.CharConversionException: isHexDigit

    总结起来,当遇到"java.io.CharConversionException: isHexDigit"异常时,应该检查前端传递的URL参数是否正确编码,并确保在服务器端使用正确的解码方法和字符集。使用`encodeURI`或`encodeURIComponent`函数代替`...

    字符串的encode/escape

    在Java中,我们可以使用`String`类的`getBytes()`方法配合指定的字符集进行编码转换,如`getBytes("UTF-8")`。编码的目的是为了将不可见的二进制数据转换成可读的字符形式,或者为了适应特定环境或平台的字符表示...

    javascript加解密字符串

    - **JavaScript编码**:可能包含使用JavaScript内置的`encodeURI()`、`encodeURIComponent()`、`escape()`等函数进行编码的示例,这些函数常用于处理URL或特殊字符。 - **Unicode与ASCII**:JavaScript中的字符串...

    JavaScript字符集编码与解码详谈

    本文将详细介绍JavaScript中的字符集,包括字符集的定义、字符与字节的关系、常见字符集类型、字符集编码与解码的过程、以及如何在浏览器中应用字符集编码。 一、字符集基础 字符集是由一套字符所组成的集合,每个...

    escape --AJAX和JS传中文乱码的最终解决方案

    在发送请求之前,可以先将中文字符串转换为GBK编码,然后使用"escape"函数进行编码。在接收端,再将其解码回GBK,从而避免乱码。 以下是一个简单的示例,展示了如何使用JavaScript处理AJAX请求中的中文乱码问题: ...

    js escape,unescape解决中文乱码问题的方法.docx

    JavaScript中的`escape`和`unescape`函数是处理字符串编码和解码的两个关键方法,尤其是在处理包含非ASCII字符(如中文字符)的字符串时。这两个函数在处理字符串时遵循特定的规则,对于理解JavaScript字符串编码...

    【JavaScript源代码】基于网址URL中特殊字符转义编码.docx

    【JavaScript源代码】基于网址URL中特殊字符转义编码是一个重要的概念,特别是在Web开发中,因为URL是连接互联网资源的基本途径。URL编码是确保数据在URL中正确传递的关键步骤,尤其是当URL包含特殊字符或者非ASCII...

    JS编码-解码.asp(C#)对应解码-编码【精品文档】.doc

    在C#中,对应JavaScript的`unescape`是`decodeURI`,它可以解码由`encodeURI`或`escape`编码的字符串。此外,C#的`Server.UrlDecode`方法用于解码由`Server.UrlEncode`编码的字符串,后者是C#中用于URL编码的标准...

Global site tag (gtag.js) - Google Analytics