zoukankan      html  css  js  c++  java
  • 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);
        }
    
  • 相关阅读:
    effective c++ 条款10 让operator= 返回*this的引用
    基于vs2008的opengl开发环境的配置
    effective c++ 条款13 以对象管理资源
    effective C++ 条款06如果你不想让编译器为你生成函数就应该明确拒绝
    effective c++条款07为多态基类声明为virtual析构函数
    effective c++条款08别让异常逃离析构函数
    effective c++条款11 在operator=中处理“自我赋值”
    ubuntu 下的截图工具
    effective c++ 条款12 赋值对象时勿忘其每一个成员
    [linux 安装]修改centos的yum源
  • 原文地址:https://www.cnblogs.com/mzq123/p/11516150.html
Copyright © 2011-2022 走看看