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 (%)的更多相关文章

  1. 【Java】Java URLDecoder异常Illegal hex characters in escape (%)

    如果收到的HTTP请求参数(URL中的GET请求)中有一个字符串,是中文,比如“10%是黄段子”,服务器段使用URLDecoder.decode就会出现此异常.URL只能使用英文字母.阿拉伯数字和某些 ...

  2. URLDecoder: Illegal hex characters in escape (%) pattern - For input string

    原因:后台发布文章的时候,内容里面有%,导致后台URLDecoder.decode()转码的时候报错. 看了java.net.URLDecoder的decode()的源码,原来是转码错误. 贴出部分代 ...

  3. java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " 0"

    value = URLDecoder.decode(request.getParameter(paraName), "UTF-8"); 前端用了 encodeURI 来编码参数,后 ...

  4. java转换编码报错java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern

    Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex cha ...

  5. java上传附件含有%处理或url含有%(URLDecoder: Illegal hex characters in escape (%) pattern - For input string)

    在附件名称中含有%的时候,上传附件进行url编码解析的时候会出错,抛出异常: Exception in thread "main" java.lang.IllegalArgumen ...

  6. 【URLDecoder】java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in es

    Java调用 URLDecoder.decode(str, "UTF-8"); 抛出以上的异常,其主要原因是% 在URL中是特殊字符,需要特殊转义一下, 上面的字符串中'%'是一个 ...

  7. URLDecoder异常解决方法

    URLDecoder对参数进行解码时候,代码如: URLDecoder.decode(param,"utf-8"); 有时候会出现类似如下的错误: URLDecoder异常Ille ...

  8. 对hadoop 执行mapreduce时发生异常Illegal partition for的解决过程

    来自:http://blog.csdn.net/hezuoxiang/article/details/6878026 写了个mapreduce的JAVA程序,自定义了个partition class ...

  9. Swagger2异常:Illegal DefaultValue null for parameter type integer java

    一.异常分析: Illegal DefaultValue null for parameter type integer`和`NumberFormatException: For input stri ...

随机推荐

  1. MySQL数据库~~~~~创建用户和授权、备份和还原

    一 MySQL创建用户和授权 1.1 对新用户增删改 1.创建用户: # 指定ip:192.118.1.1的chao用户登录 create user 'chao'@'192.118.1.1' iden ...

  2. Django实现标签联动以及xadmin中实现标签联动

    如图,即实现点击一个城市,出现对应的学校名称.开始一直以为是建立数据表的时候实现的,原来是通过ajax实现的. 思路:当get请求显示原始状态(即下拉框呈现全部内容).当点击一个城市后,通过ajax的 ...

  3. element-ui 中Switch的用法

    在element-ui中,如果你想知道Switch是开还是关,使用事件 @change="getchange(value2)" 它会输出true或者false.true代表的是开, ...

  4. Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】

    Help Hanzo (LightOJ - 1197) [简单数论][筛区间质数] 标签: 入门讲座题解 数论 题目描述 Amakusa, the evil spiritual leader has ...

  5. IDEA生成可执行的jar文件

    场景 用IDEA开发一个Java控制台程序,项目完成后,打包给客户使用. 做法 首先用IDEA打开要生成jar的项目,打开后选择File->Project Structure... 选择Arti ...

  6. go语言之数组

    1.go语言的数组和python的列表不一样,python的列表没有限定类型,而go的数组限定的类型,理由是这样的,请看下面的图 go语言的数组会数组中第一个值的内存地址,在我们上面的例子中,数组中的 ...

  7. Java 的核心目的和并发编程

    读一本书,最好能从它的前言开始.那么我们就来看看<Java编程思想>作者 Bruce Eckel 在前言里都说了些什么吧. 01.Java 的核心目的是"为程序员减少复杂性&qu ...

  8. IE浏览器远程代码执行高危漏洞(CVE-2019-1367)

    IE浏览器远程代码执行高危漏洞(CVE-2019-1367)加固遇到的问题 一.背景介绍 Internet Explorer,是微软公司推出的一款网页浏览器.用户量极大.9月23日微软紧急发布安全更新 ...

  9. asp.net MVC通用权限管理系统-响应式布局-源码

    一.Angel工作室简单通用权限系统简介 AngelRM(Asp.net MVC Web api)是基于asp.net(C#)MVC+前端bootstrap+ztree+lodash+jquery技术 ...

  10. 资深程序员告诉你为什么要用Python3而不是Python2

    经常遇到这样的问题:<现在开始学习python的话,是学习python2.x还是学习python3.x比较好?>,这也是许多初学者会遇到的问题,我们的答案是python 3.x. 为了帮助 ...