`
knight_black_bob
  • 浏览: 858801 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JavaScript escape/unescape 编码的 Java 实现

    博客分类:
  • java
阅读更多

 

 

/** 
 * 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)]);  
            }  
        }  
        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 = "S#1234567abcd[]()<+>,.~//";  
        System.out.println(stest);  
        System.out.println(escape(stest));  
        System.out.println(unescape(escape(stest)));
    }  
}  

 

 

S#1234567abcd[]()<+>,.~//
S%231234567abcd%5B%5D()%3C%2B%3E%2C.~//
S#1234567abcd[]()<+>,.~//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

分享到:
评论

相关推荐

    java实现js的escape和unescape

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

    java版本的escape和unescape函数

    在Java编程语言中,`escape`和`unescape`函数是处理字符串时常见的工具,尤其在处理HTML、JavaScript或URL编码时。这两个函数主要用于在字符串中转换特殊字符,确保数据在网络传输或存储时的正确性和安全性。下面...

    Java实现JS中的escape和UNescape代码分享

    总结来说,Java虽然没有内置的 `escape` 和 `unescape` 函数,但通过自定义函数,我们可以实现与JavaScript中类似的功能。同时,对于字符编码的转换,Java提供了强大的支持,如上述的 `isoToGB` 和 `isoToUTF` 方法...

    base64,java与JavaScript实现

    ### 关于Base64编码及Java与JavaScript的实现 #### Base64简介 Base64是一种将二进制数据转换为ASCII字符串表示形式的方法。它主要用于处理非文本数据的存储和传输,例如图像、视频等多媒体文件。由于互联网传输...

    java和js相互base64编码解码

    总之,Java和JavaScript中Base64编码和解码的实现原理相似,都是将二进制数据转换为可打印的ASCII字符。Java自Java 8开始内置了Base64支持,而JavaScript则依赖于`btoa()`和`atob()`函数,或者使用第三方库。在实际...

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

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

    字符串的encode/escape

    在编程语言中,例如JavaScript、Python或Java,有一些字符具有特殊含义,如`"`, `\`, `\n`, `\r`等。为了避免这些字符被解释为特殊含义,我们需要在它们前面加上反斜杠`\`,这就是转义。在Java中,`java.net....

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

    `unescape`是JavaScript中用于解码`escape`编码的字符串,但在PHP中没有直接对应的功能,因此需要自定义函数如`phpUnescape`来实现类似的功能。 总的来说,这篇文章探讨了一个实用的PHP函数,该函数可以正确地解码...

    java 中文乱码 处理

    以上介绍了几种常见的Java中文乱码处理方法,包括请求参数编码转换、字符串转义与反转义、前端JavaScript处理以及配置J2EE应用程序。每种方法都有其适用场景,开发者可以根据实际情况选择合适的方法来解决乱码问题。...

    JSP页面中文传递参数使用escape编码

    需要注意的是,使用escape/unescape方法进行编码和解码是JavaScript特有的,并不是Java标准库中的方法。因此,确保在JSP页面中获取到的是正确编码的字符串,且在客户端和服务器端的编码和解码方法要保持一致。 另外...

    解决java中文乱码全集

    本文将详细讲解几种常见的解决Java中文乱码的方法,包括如何正确处理请求参数中的中文字符、如何利用`escape()`与`unescape()`方法进行字符串编码转换、JavaScript中的编码处理技巧以及J2EE环境下系统全局的字符编码...

    javascript URL编码和解码使用说明

    javaScript中的编码方法: escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码 (xx等于该字符在字符集表里面的编码...

    很有价值的javascript学习笔记

    - **字符串编码与解码**:`window.escape()` 用于对字符串进行编码,`unescape()` 用于解码。 - **函数调用**:可以通过指向函数的变量来调用函数。例如,`var myFunc = function() { ... }; myFunc();`。 - **...

    彻底解决jsp中用SmartUpload上传附件中文乱码问题实用.pdf

    1. **上传前编码**:在客户端(通常是HTML表单)中,可以使用JavaScript的`escape()`函数对文件名进行编码。例如,文件输入字段的值在提交前先通过JavaScript进行编码: ```html 上传附件" onClick="doFileUpLoad...

    JavaScript中的编码和解码函数

    - `unescape()` 是 `escape()` 的对应解码函数,它将编码的字符串恢复为原始格式。它能识别并解码由 `escape()` 创建的编码字符串,包括 `%uXXXX` 形式的Unicode字符。 5. **decodeURI() 函数**: - `decodeURI()...

    地址栏传递中文参数乱码在js里用escape转码

    根据给出的代码示例,可以通过Escape类中的unescape静态方法来实现。这个类中的unescape方法将使用十六进制转义字符数组和对应的值数组来完成解码。具体代码如下: ```java package com.laneasy.hy.util; /** * ...

    学习java开发中的特殊字符.pdf

    在JavaScript中,URL编码和解码的函数分别是`escape(String s)`和`unescape(String s)`。不过,现代JavaScript更推荐使用`encodeURIComponent()`和`decodeURIComponent()`来编码和解码URL,因为它们更全面,能正确...

Global site tag (gtag.js) - Google Analytics