package com.util;
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");
}
}
分享到:
相关推荐
主要介绍了java 中类似js encodeURIComponent 函数的实现案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
同学的毕业设计出现JavaScript用encodeURIComponentt编码后无法再后台解码的问题。 原来他是这样写的: window.self.location="searchbytext.action?searchtext="+encodeURIComponent(seartext);
主要介绍了JavaScript、C# URL编码总结,注意包括了encodeURI、decodeURI、encodeURIComponent、decodeURIComponent等使用需要的朋友可以参考下
serialize , encodeURIComponent encodeURI 中文转成GBK编码 encodeURIComponent encodeURI 默认转 utf-8 ;重写方法 转成GBK
escape()、encodeURI()、encodeURIComponent()区别详解
encodeURIComponent(encodeURIComponent(customerAddress)) decodeURIComponent(customerName) js到java encodeURI(url) String qijuType= new String(request.getParameter( ("qijuType")).getBytes("ISO-...
url传递汉字的解决方法 encodeURIComponent encodeURI 的区别
首先,我们都知道这三个东西都是用来编码的先来说encodeURI()和encodeURIComponent(),这两个是在转换url时候用来编码解码用的。 有编码就会有解码,解码就是decodeURI()和decodeURIComponent(),他们的用法很简单,...
encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给 decodeURIComponent,那么将返回初始的字符串
这个时候,出现了encodeURIComponent、decodeURIComponent,它可以完全的对URL进行编码解码,但是遇到例如搜索引擎用到的部分转码,又摸不到门了,没问题,PHP官方出了一个解决方案: 代码如下: decodeURIComponent...
在JS中使用了encodeURIComponent对中文进行编码在PHP中使用iconv('UTF-8','gb2312',$q);就可以得到你需要的字串了,其中gb2312根据你实际应用来定如还不明白为什么看下面的
语言:中文 (简体) 集成了JSON格式化、base64编解码、encodeURIComponent、decodeURIComponent等功能 集成了JSON格式化、base64编解码、encodeURIComponent、decodeURIComponent、百度翻译等功能
Javascript中escape(),_encodeURI()和encodeURIComponent()之精析与比较.doc
很简单,一看就懂,用不明白可以留言,看到必回!如果加密中文js里加密之前encodeURIComponent编码一下,java用java.net.URLDecoder.decode(outputStr, "UTF-8");解码
在JavaScript中对字符串进行转义和反转义操作,常用的方法莫过于使用encodeURI (decodeURI)、encodeURIComponent (decodeURIComponent)这几个方法,具体使用方法和区别。 但是如何在JavaScript中对HTML进行反转义...
背景 encodeURI 和 encodeURIComponent都是ECMA-262标准中定义的函数,所有兼容这个标准的语言(如JavaScript, ActionScript)都会实现这两个函数。它们都是用来对URI (RFC-2396)字符串进行编码的全局函数,但是...
NULL 博文链接:https://liuzidong.iteye.com/blog/829204
javascript的Global对象(javascript的内置对象)中有四个URI方法,分别是encodeURI和decodeURI,encodeURIComponent和decodeURIComponent,浏览器模型(BOM)提供了escape和unescape。在实际应用中,URI方法更可取...
escape() 方法:采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。...