digdeep

凡是过去,皆是序幕。Read the fucking manual and source code.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

value = URLDecoder.decode(request.getParameter(paraName), "UTF-8");

前端用了 encodeURI 来编码参数,后端用 URLDecoder 解码,报错:

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

 

http://stackoverflow.com/questions/6067673/urldecoder-illegal-hex-characters-in-escape-pattern-for-input-string

Characters that get encoded have % and + signs in them, so although this helps with % and + characters in a string, it also doesn't decode things like %20 (space) because you are taking out the percent before decoding.

A solution is to replace %2B (+) and %25 (%) instead. Something like:

   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;
   }

"+" is a special character which denotes a quantifier meaning one of more occurrences. So one should use "\+"

 

posted on 2017-02-14 15:48  digdeep  阅读(7587)  评论(1编辑  收藏  举报
不懂数据库和Web安全的架构师不是一个好的程序员。