zoukankan      html  css  js  c++  java
  • java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern

    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 "+"

  • 相关阅读:
    第12章学习笔记
    尝试用华为Matepad平板在华为云openEuler做SM系列测试实验
    flex tree xml相关
    asp.net乱码问题
    ArcGIS Server for Flex 资源收集
    asp.net 读写excel
    Geoprocessor 使用
    上传控件
    html页面布局 水平居中 垂直居中
    ArcGIS Engine 代码收集贴
  • 原文地址:https://www.cnblogs.com/digdeep/p/6397962.html
Copyright © 2011-2022 走看看