public class EncodingHelperImpl implements EncodingHelper {
/**
* js unescape
*
* @param str
* @return
*/
public String StringToUnicode(String str) throws Exception {
String tmp;
StringBuffer sb = new StringBuffer(1000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c > 255) {
sb.append("%u");
j = (c >>> 8);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
} else {
sb.append(c);
}
}
return (new String(sb));
}
/**
* js escape
*
* @param str
* @return
*/
public String UnicodeToString(String str) throws Exception {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
return str;
}
/**
* encodeURIComponent
*
* @param param
* @return
*/
public String decode(String param) throws Exception {
try {
String decode = java.net.URLDecoder.decode(param, "utf-8");
return decode;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return param;
}
/**
* decodeURIComponent
*
* @param param
* @return
*/
public String encode(String param) throws Exception {
try {
String decode = java.net.URLEncoder.encode(param, "utf-8");
return decode;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return param;
}
}
web乱码
最新推荐文章于 2024-08-26 16:40:46 发布