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

java模拟js的escape和unescape函数

    博客分类:
  • util
阅读更多

这个是在网上找的代码,然后修改了下.作用标题已经很明显了.

/**
 * java模拟js的escape和unescape函数.
 * 
 * 
 */
public class JSEscape {
	private 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 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 };

	/**
	 * 编码,模拟js的escape函数.<br>
	 * escape不编码字符有69个:*+-./@_0-9a-zA-Z
	 * 
	 * @param s
	 *            字符串
	 * @return 转义后的字符串或者null
	 */
	public static String escape(String s) {
		if (s == null) {
			return null;
		}
		StringBuffer sbuf = new StringBuffer();
		int len = s.length();
		for (int i = 0; i < len; i++) {
			int ch = s.charAt(i);
			if ('A' <= ch && ch <= 'Z') {
				sbuf.append((char) ch);
			} else if ('a' <= ch && ch <= 'z') {
				sbuf.append((char) ch);
			} else if ('0' <= ch && ch <= '9') {
				sbuf.append((char) ch);
			} else if (ch == '*' || ch == '+' || ch == '-' || ch == '/'
					|| ch == '_' || ch == '.' || ch == '@') {
				sbuf.append((char) ch);
			} else if (ch <= 0x007F) {
				sbuf.append('%');
				sbuf.append(hex[ch]);
			} else {
				sbuf.append('%');
				sbuf.append('u');
				sbuf.append(hex[(ch >>> 8)]);
				sbuf.append(hex[(0x00FF & ch)]);
			}
		}
		return sbuf.toString();
	}

	/**
	 * 解码,模拟js的unescape函数. 说明:本方法保证 不论参数s是否经过escape()编码,均能得到正确的“解码”结果
	 * 
	 * @param s
	 * @return
	 */
	public static String unescape(String s) {
		if (s == null) {
			return null;
		}
		StringBuffer sbuf = new StringBuffer();
		int i = 0;
		int len = s.length();
		while (i < len) {
			int ch = s.charAt(i);
			if ('A' <= ch && ch <= 'Z') {
				sbuf.append((char) ch);
			} else if ('a' <= ch && ch <= 'z') {
				sbuf.append((char) ch);
			} else if ('0' <= ch && ch <= '9') {
				sbuf.append((char) ch);
			} else if (ch == '*' || ch == '+' || ch == '-' || ch == '/'
					|| ch == '_' || ch == '.' || ch == '@') {
				sbuf.append((char) ch);
			} else if (ch == '%') {
				int cint = 0;
				if ('u' != s.charAt(i + 1)) {
					cint = (cint << 4) | val[s.charAt(i + 1)];
					cint = (cint << 4) | val[s.charAt(i + 2)];
					i += 2;
				} else {
					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);
			} else {
				sbuf.append((char) ch);
			}
			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)));
		System.out
				.println(unescape("%7B%27id%27%3A1%2C%27parentId%27%3A2%2C%27name%27%3A%27%u6D4B%u8BD5%u6837%u4F8B%27%2C%27dataSource%27%3A%2720%27%2C%27path%27%3A%272/1.rep%27%7D"));
		System.out
				.println(escape("{'id':1,'parentId':2,'name':'测试样例','dataSource':'20','path':'2/1.rep'}"));
	}
}
 
分享到:
评论

相关推荐

    java版本的escape和unescape函数

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

    JavaScript escape/unescape 编码的 Java 实现

    本工具类用java实现了JavaScript escape/unescape 编码。对接建行支付以及需要向后端传输中文数据时可以使用得到

    java实现js的escape和unescape

    ### Java实现JS的`escape`和`unescape`方法 #### `escape`方法:URL编码 在JavaScript中,`escape()`函数用于将字符串转换为适合在网络上传输的格式,主要通过将特殊字符(如空格、引号等)转换为百分号编码的形式...

    asp函数实现javascript里的escape函数和unescape函数功能

    通过上述两个函数,我们可以在ASP环境中模拟JavaScript的`escape`和`unescape`功能,从而实现字符串的安全编码与解码。这对于处理包含特殊字符的字符串非常有用,尤其是在需要与JavaScript或其他Web技术进行交互的...

    escape/unescape 编码的Java 实现

    使用java对字符串进行unicode编码和解码。源程序

    PHP实现Javascript中的escape及unescape函数代码分享_.docx

    此外,还提供了一个`phpescape`函数,其原理与`escape`相似,但使用了`iconv`函数和`bin2hex`来处理GBK编码的多字节字符: ```php function phpescape($str) { // ... } ``` 最后,`unescape`的实现还包括一个...

    js中的escape及unescape函数的php实现代码.docx

    在JavaScript中,`escape()`和`unescape()`函数是用来处理字符串的编码和解码的。它们在处理URL编码和特定字符编码时非常有用。然而,PHP并没有直接等效的内置函数,但我们可以根据其功能编写相应的PHP实现。下面将...

    PHP实现Javascript中的escape及unescape函数代码分享

    在JavaScript中,`escape`和`unescape`函数是用来对字符串进行编码和解码的,主要是为了处理非ASCII字符。然而,在PHP中,没有直接对应的内置函数。但是,通过自定义函数,我们可以模拟这些功能。这里我们将深入探讨...

    如何使用PHP实现javascript的escape和unescape函数

    在进行Web开发时,经常需要处理不同语言编码的字符串,...上述提供的“escape”和“unescape”函数能够帮助开发者在PHP环境中模拟JavaScript的这两个函数,从而在PHP和JavaScript交互时,保证字符串的正确编码和解码。

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

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

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

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

    PHP实现JS中escape与unescape的方法

    首先,本文主要讲述了在PHP中实现JavaScript中escape和unescape函数功能的方法。JavaScript中的escape函数可以将字符串转换为unicode十六进制编码,而unescape函数则可以将这种编码转换回原始字符串。由于JSON的编码...

    android 实现JavaScript escape unescape

    输入样例: String stest = "中文1234 abcd[](),.~\\";...System.out.println(unescape(escape(stest))); 输出结果: 中文1234 abcd[](),.~\ %u4E2D%u65871234+abcd%5B%5D()%3C%2B%3E%2C.~%5C 中文1234 abcd[](),.~\

    js中的escape及unescape函数的php实现代码

    JavaScript 提供了 `escape` 和 `unescape` 这两个原生函数来完成这样的任务。然而,在某些情况下,我们可能需要在服务器端(如 PHP)模拟这些功能。本文将详细介绍如何在 PHP 中实现 JavaScript 的 `escape` 和 `...

    php模拟js函数unescape的函数代码

    PHP模拟JavaScript的unescape函数的实现涉及到了URL编码和Unicode字符的转换。unescape函数是一个JavaScript内置函数,用于将百分号编码的字符串转换为相应的字符。在PHP中,虽然没有内置的unescape函数,但可以通过...

    Escape.java_java_

    - **JSON或XML转义**:如果`Escape.java`用于处理这些格式,那么它可能包含了对`, `&gt;`, `&`, `"`和`'`的转义和unescape功能。 6. **字符集转换**: `Escape.java`可能包含将字符串从一种字符集转换到另一种字符集...

    sqlserver sql 实现 escape 解码

    总的来说,虽然SQL Server没有内置的`unescape`函数,但通过T-SQL的字符串操作,我们可以构建自己的解码逻辑来模拟这一功能,以便在数据库层面上处理经过`escape`编码的字符串。这对于处理前端传递的数据或者生成套...

    C#版的 Escape() 和 Unescape() 函数分享

    提到的 `Microsoft.JScript.GlobalObject.escape("")` 和 `Microsoft.JScript.GlobalObject.unescape("")` 是.NET框架中对JavaScript内置函数的引用。在JavaScript中,`escape()` 和 `unescape()` 也执行类似的字符...

    C++ Builder XE BCB escape unescape 源码 演示

    在这个特定的示例中,我们关注的是如何在C++ Builder XE中实现JavaScript的`escape`和`unescape`功能。这两个函数在JavaScript中广泛用于对字符串进行编码和解码,以便在网络传输或存储时能够正确处理特殊字符。 `...

Global site tag (gtag.js) - Google Analytics