`
yzz9i
  • 浏览: 217220 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

encodeURIComponent 与 decodeURIComponent 编码互转

 
阅读更多
package com.file;


import java.io.UnsupportedEncodingException;
 
public class UrlDeal {
	public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
 
	public static String encodeURIComponent(String input) {
		if (input == null || "".equals(input)) {
			return input;
		}
 
		int l = input.length();
		StringBuilder o = new StringBuilder(l * 3);
		try {
			for (int i = 0; i < l; i++) {
				String e = input.substring(i, i + 1);
				if (ALLOWED_CHARS.indexOf(e) == -1) {
					byte[] b = e.getBytes("utf-8");
					o.append(getHex(b));
					continue;
				}
				o.append(e);
			}
			return o.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return input;
	}
 
	private static String getHex(byte buf[]) {
		StringBuilder o = new StringBuilder(buf.length * 3);
		for (int i = 0; i < buf.length; i++) {
			int n = (int) buf[i] & 0xff;
			o.append("%");
			if (n < 0x10) {
				o.append("0");
			}
			o.append(Long.toString(n, 16).toUpperCase());
		}
		return o.toString();
	}
 
	public static String decodeURIComponent(String encodedURI) {
		char actualChar;
 
		StringBuffer buffer = new StringBuffer();
 
		int bytePattern, sumb = 0;
 
		for (int i = 0, more = -1; i < encodedURI.length(); i++) {
			actualChar = encodedURI.charAt(i);
 
			switch (actualChar) {
			case '%': {
				actualChar = encodedURI.charAt(++i);
				int hb = (Character.isDigit(actualChar) ? actualChar - '0'
						: 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
				actualChar = encodedURI.charAt(++i);
				int lb = (Character.isDigit(actualChar) ? actualChar - '0'
						: 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
				bytePattern = (hb << 4) | lb;
				break;
			}
			case '+': {
				bytePattern = ' ';
				break;
			}
			default: {
				bytePattern = actualChar;
			}
			}
 
			if ((bytePattern & 0xc0) == 0x80) { // 10xxxxxx
				sumb = (sumb << 6) | (bytePattern & 0x3f);
				if (--more == 0)
					buffer.append((char) sumb);
			} else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxx
				buffer.append((char) bytePattern);
			} else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxx
				sumb = bytePattern & 0x1f;
				more = 1;
			} else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxx
				sumb = bytePattern & 0x0f;
				more = 2;
			} else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxx
				sumb = bytePattern & 0x07;
				more = 3;
			} else if ((bytePattern & 0xfc) == 0xf8) { // 111110xx
				sumb = bytePattern & 0x03;
				more = 4;
			} else { // 1111110x
				sumb = bytePattern & 0x01;
				more = 5;
			}
		}
		return buffer.toString();
	}
	public static void main(String[] arges){
		System.out.println(decodeURIComponent("%E4%BD%A0%E5%A5%BD%20%E7%9C%9F%E7%9A%84"));
		System.out.println(encodeURIComponent("真的"));
		System.out.println("%E4%BD%A0%E5%A5%BD%20%E7%9C%9F%E7%9A%84");
	}
}
0
0
分享到:
评论

相关推荐

    关于JAVASCRIPT urldecode URL解码的问题

    这个时候,出现了encodeURIComponent、decodeURIComponent,它可以完全的对URL进行编码解码,但是遇到例如搜索引擎用到的部分转码,又摸不到门了,没问题,PHP官方出了一个解决方案: 代码如下: decodeURIComponent...

    JavaScript、C# URL编码、解码总结

    主要介绍了JavaScript、C# URL编码总结,注意包括了encodeURI、decodeURI、encodeURIComponent、decodeURIComponent等使用需要的朋友可以参考下

    谈谈encodeURI和encodeURIComponent以及escape的区别与应用

    首先,我们都知道这三个东西都是用来编码的先来说encodeURI()和encodeURIComponent(),这两个是在转换url时候用来编码解码用的。 有编码就会有解码,解码就是decodeURI()和decodeURIComponent(),他们的用法很简单,...

    javascript url几种编码方式详解

    1. escape() 不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值。...3. encodeURIComponent()能编码”;/?:@&=+$,#”这些特殊字符。对应的解码函数decodeURIComponent()。假如要传递带&符号的网址,所以

    js中编码函数:escape,encodeURI与encodeURIComponent详解

    其它情况下escape,encodeURI,encodeURIComponent编码结果相同。  escape对0-255以外的unicode值进行编码时输出%u****格式  可以使用 unescape() 对 escape() 编码的字符串进行解码。  ECMAScript v3 反对使用该...

    JS中encodeURIComponent函数用php解码的代码

    在JS中使用了encodeURIComponent对中文进行编码在PHP中使用iconv('UTF-8','gb2312',$q);就可以得到你需要的字串了,其中gb2312根据你实际应用来定如还不明白为什么看下面的

    在asp中使用js的encodeURIComponent方法

    encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给 decodeURIComponent,那么将返回初始的字符串

    UTF-8 Unicode Ansi 汉字GB2321几种编码转换程序

    今天搞sxna,遇到了编码转换... ——————————————————————————– 符合GOOGLE的UTF编码 汉字 经过encodeURIComponent变成 汉字 汉字经过decodeURIComponent变成汉字 用google搜索”汉字”: ...

    js中字符串编码函数escape()、encodeURI()、encodeURIComponent()区别详解

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数: unescape,decodeURI,decodeURIComponent 。 下面简单介绍一下它们的区别 1 escape()函数 定义和...

    字符串编码的机器

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 。 javascript的简单编码机器和解码机器二合一

    深入分析escape()、encodeURI()、encodeURIComponent()的区别及示例

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape, decodeURI, decodeURIComponent 。 下面简单介绍一下它们的区别: 1 escape()函数 定义和...

    关于URL中的特殊符号使用介绍

    javascript的Global对象(javascript的内置对象)中有四个URI方法,分别是encodeURI和decodeURI,encodeURIComponent和decodeURIComponent,浏览器模型(BOM)提供了escape和unescape。在实际应用中,URI方法更可取...

    JavaScript中的编码和解码函数

    js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断...

    js中encode、decode的应用说明

    如果您将编码结果传递给decodeURIComponent,那么将返回初始的字符串。因为encodeURIComponent方法对所有的字符编码,请注意,如果该字符串代表一个路径,例如/folder1/folder2/default.html,其中的斜杠也将被编码...

    JS前端知识点 运算符优先级,URL编码与解码,String,Math,arguments操作整理总结

    本文实例讲述了JS前端知识点 运算符优先级,URL编码与解码,String,Math,arguments操作。分享给大家供大家参考,具体如下: Js 中的运算符优先级 优先级依次降低 () !、-、++、– *、/、% +、- &lt;、&lt;=...

    Microsoft.JScript 下载

    Microsoft.JScript 下载。网上找到的。很实用。注意解码方式。...后台使用 Microsoft.JScript.GlobalObject.encodeURIComponent(要编码的字符串) 前台使用 decodeURIComponent(要解码的字符串)

    js字符编码函数区别分析

    js对文字编码有3个函数: escape,encodeURI,encodeURIComponent, 对应的解码函数:unescape,decodeURI,decodeURIComponent

    utf8-regex-encode-decode-js:Utf8字符串使用正则表达式进行编码

    可以通过unescape(encodeURIComponent(str))在JavaScript中实现相同的功能,但是这种方法在其他语言中可能很有用。 可以将utf-8编码的字符串解码回常规的javascript字符串。 JavaScript也可以通过...

Global site tag (gtag.js) - Google Analytics