/**
* 解码
*
* @param src
* @return
*/
public static String unescape(String src) {
if (null == src) {
return "";
}
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0;
int pos = 0;
char ch;
// 查找%,进行解码
while (lastPos < src.length()) {
pos = src.indexOf('%', lastPos);
// 是经过转玛的字符
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u')// 是中文
{
ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else
// 是ascil码
{
ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else
// 不需要解码
{
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
URL解码
最新推荐文章于 2024-07-30 12:22:43 发布