当前位置 博文首页 > 文章内容

    URLDecoder异常解决方法

    作者: 栏目:未分类 时间:2020-08-05 18:01:17

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    URLDecoder对参数进行解码时候,代码如:

    URLDecoder.decode(param,"utf-8");

    有时候会出现类似如下的错误:

    URLDecoder异常Illegal hex characters in escape (%)

    这是因为传参有一些特殊字符,比如%号或者说+号,导致不能解析,报错

    解决方法是:

     1 public static String replacer(StringBuffer outBuffer) {
     2       String data = outBuffer.toString();
     3       try {
     4          data = data.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
     5          data = data.replaceAll("\\+", "%2B");
     6          data = URLDecoder.decode(data, "utf-8");
     7       } catch (Exception e) {
     8          e.printStackTrace();
     9       }
    10       return data;
    11    }

    URLDecoder源码:

     1 public static String decode(String s, String enc)
     2         throws UnsupportedEncodingException{
     3 
     4         boolean needToChange = false;
     5         int numChars = s.length();
     6         StringBuffer sb = new StringBuffer(numChars > 500 ? numChars / 2 : numChars);
     7         int i = 0;
     8 
     9         if (enc.length() == 0) {
    10             throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
    11         }
    12 
    13         char c;
    14         byte[] bytes = null;
    15         while (i < numChars) {
    16             c = s.charAt(i);
    17             switch (c) {
    18             case '+':
    19                 sb.append(' ');
    20                 i++;
    21                 needToChange = true;
    22                 break;
    23             case '%':
    24                 /*
    25                  * Starting with this instance of %, process all
    26                  * consecutive substrings of the form %xy. Each
    27                  * substring %xy will yield a byte. Convert all
    28                  * consecutive  bytes obtained this way to whatever
    29                  * character(s) they represent in the provided
    30                  * encoding.
    31                  */
    32 
    33                 try {
    34 
    35                     // (numChars-i)/3 is an upper bound for the number
    36                     // of remaining bytes
    37                     if (bytes == null)
    38                         bytes = new byte[(numChars-i)/3];
    39                     int pos = 0;
    40 
    41                     while ( ((i+2) < numChars) &&
    42                             (c=='%')) {
    43                         int v = Integer.parseInt(s.substring(i+1,i+3),16);
    44                         if (v < 0)
    45                             throw new IllegalArgumentException("URLDecoder: Illegal hex characters in escape (%) pattern - negative value");
    46                         bytes[pos++] = (byte) v;
    47                         i+= 3;
    48                         if (i < numChars)
    49                             c = s.charAt(i);
    50                     }
    51 
    52                     // A trailing, incomplete byte encoding such as
    53                     // "%x" will cause an exception to be thrown
    54 
    55                     if ((i < numChars) && (c=='%'))
    56                         throw new IllegalArgumentException(
    57                          "URLDecoder: Incomplete trailing escape (%) pattern");
    58 
    59                     sb.append(new String(bytes, 0, pos, enc));
    60                 } catch (NumberFormatException e) {
    61                     throw new IllegalArgumentException(
    62                     "URLDecoder: Illegal hex characters in escape (%) pattern - "
    63                     + e.getMessage());
    64                 }
    65                 needToChange = true;
    66                 break;
    67             default:
    68                 sb.append(c);
    69                 i++;
    70                 break;
    71             }
    72         }
    73 
    74         return (needToChange? sb.toString() : s);
    75     }