URLDecoder异常Illegal hex characters in escape (%)
URLDecoder对参数进行解码时候,代码如:
URLDecoder.decode(param,"utf-8");
有时候会出现类似如下的错误:
URLDecoder异常Illegal hex characters in escape (%)
这是因为传参有一些特殊字符,比如%号或者说+号,导致不能解析,报错
解决方法是:
public static String replacer(StringBuffer outBuffer) {
String data = outBuffer.toString();
try {
data = data.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
data = data.replaceAll("\\+", "%2B");
data = URLDecoder.decode(data, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
URLDecoder源码:
public static String decode(String s, String enc)
throws UnsupportedEncodingException{
boolean needToChange = false;
int numChars = s.length();
StringBuffer sb = new StringBuffer(numChars > 500 ? numChars / 2 : numChars);
int i = 0;
if (enc.length() == 0) {
throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
}
char c;
byte[] bytes = null;
while (i < numChars) {
c = s.charAt(i);
switch (c) {
case '+':
sb.append(' ');
i++;
needToChange = true;
break;
case '%':
/*
* Starting with this instance of %, process all
* consecutive substrings of the form %xy. Each
* substring %xy will yield a byte. Convert all
* consecutive bytes obtained this way to whatever
* character(s) they represent in the provided
* encoding.
*/
try {
// (numChars-i)/3 is an upper bound for the number
// of remaining bytes
if (bytes == null)
bytes = new byte[(numChars-i)/3];
int pos = 0;
while ( ((i+2) < numChars) &&
(c=='%')) {
int v = Integer.parseInt(s.substring(i+1,i+3),16);
if (v < 0)
throw new IllegalArgumentException("URLDecoder: Illegal hex characters in escape (%) pattern - negative value");
bytes[pos++] = (byte) v;
i+= 3;
if (i < numChars)
c = s.charAt(i);
}
// A trailing, incomplete byte encoding such as
// "%x" will cause an exception to be thrown
if ((i < numChars) && (c=='%'))
throw new IllegalArgumentException(
"URLDecoder: Incomplete trailing escape (%) pattern");
sb.append(new String(bytes, 0, pos, enc));
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"URLDecoder: Illegal hex characters in escape (%) pattern - "
+ e.getMessage());
}
needToChange = true;
break;
default:
sb.append(c);
i++;
break;
}
}
return (needToChange? sb.toString() : s);
}
URLDecoder异常Illegal hex characters in escape (%)的更多相关文章
- 【Java】Java URLDecoder异常Illegal hex characters in escape (%)
如果收到的HTTP请求参数(URL中的GET请求)中有一个字符串,是中文,比如“10%是黄段子”,服务器段使用URLDecoder.decode就会出现此异常.URL只能使用英文字母.阿拉伯数字和某些 ...
- URLDecoder: Illegal hex characters in escape (%) pattern - For input string
原因:后台发布文章的时候,内容里面有%,导致后台URLDecoder.decode()转码的时候报错. 看了java.net.URLDecoder的decode()的源码,原来是转码错误. 贴出部分代 ...
- java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " 0"
value = URLDecoder.decode(request.getParameter(paraName), "UTF-8"); 前端用了 encodeURI 来编码参数,后 ...
- java转换编码报错java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern
Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex cha ...
- java上传附件含有%处理或url含有%(URLDecoder: Illegal hex characters in escape (%) pattern - For input string)
在附件名称中含有%的时候,上传附件进行url编码解析的时候会出错,抛出异常: Exception in thread "main" java.lang.IllegalArgumen ...
- 【URLDecoder】java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in es
Java调用 URLDecoder.decode(str, "UTF-8"); 抛出以上的异常,其主要原因是% 在URL中是特殊字符,需要特殊转义一下, 上面的字符串中'%'是一个 ...
- URLDecoder异常解决方法
URLDecoder对参数进行解码时候,代码如: URLDecoder.decode(param,"utf-8"); 有时候会出现类似如下的错误: URLDecoder异常Ille ...
- 对hadoop 执行mapreduce时发生异常Illegal partition for的解决过程
来自:http://blog.csdn.net/hezuoxiang/article/details/6878026 写了个mapreduce的JAVA程序,自定义了个partition class ...
- Swagger2异常:Illegal DefaultValue null for parameter type integer java
一.异常分析: Illegal DefaultValue null for parameter type integer`和`NumberFormatException: For input stri ...
随机推荐
- How to Create Transportable Tablespaces Where the Source and Destination are ASM-Based (Doc ID 394798.1)
How to Create Transportable Tablespaces Where the Source and Destination are ASM-Based (Doc ID 39479 ...
- getOutputStream() has already been called for this response 从了解到解决
一.背景说明 在tomcat的localhost.log日志中时长见到 getOutputStream() has already been called for this respon ...
- 【CentOS 7】CentOS 7各个版本镜像下载地址(转)
参考链接:https://www.centos.org/download/mirrors/ https://www.cnblogs.com/defineconst/p/11176593.html
- MySQL数据库解决大数据量存储问题
转载自:https://www.cnblogs.com/ryanzheng/p/8334915.html 提问:如何设计或优化千万级别的大表?此外无其他信息,个人觉得这个话题有点范,就只好简单说下该如 ...
- 【oracle】PLS-00103: 出现符号 "end-of-file"
begin xxxxx end; 修改xxxxx为xxxxx:就好了
- ElementPath
ElementTree库附带了一个简单的类似XPath的路径语言ElementPath主要区别在于,可以在ElementPath表达式中使用{namespace}标记符号但是,诸如值比较和函数之类的高 ...
- ycsb 测试Hbase性能
下载 github:https://github.com/brianfrankcooper/YCSB/releases/tag/0.10.0 wget https://github.com/brian ...
- python del和垃圾回收
1. del是删除对象 2. python中的垃圾回收是删除引用计数
- [转]为什么group by后面不能使用别名(除MySQL)
同事工作中遇到一个问题: select count(billingdate),to_char(billingdate,'YYYYmm') month from tu_trade where to_ ...
- windows下的go get 显示进度
我的Go版本是:go1.12.7 1.在你的Go安装目录下找到 D:\Go\src\github.com\tools\godep\vendor\golang.org\x\tools\go\vcs\vs ...