urldecoder decode()代码
    URLDecoder是Java语言里的一个类,它的主要作用是将一个经过URL编码的字符串进行解码还原成一个普通字符串。这使得在网页浏览器中使用GET方式传递参数时,通过解码后可以获取到正常的参数值,这样就不会出现传递中文参数乱码的问题。
    URLDecoder类里的decode()方法是解码URL的核心方法,现在我们就来分步骤阐述该方法的具体实现过程。
    1. 首先解码第一个参数
```
public static String decode(String s, String enc) {
    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);
在线url网址编码解码        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 {
                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());
            } catch (UnsupportedEncodingException e) {
                throw new AssertionError(e);
            }
            needToChange = true;
            break;
        default:
            sb.append(c);
            i++;
            break;
        }
    }
        return (needToChange? sb.toString() : s);
}
```
    2. 参数传入方法中
我们看到,该方法有两个参数,第一个参数是需要解码的字符串s,第二个参数是编码方式enc,通常我们使用UTF-8编码方式。因此,第一步是将需要解码的字符串s和编码方式enc传递给decode()方法。
    3. 判断传递进来的编码方式enc是否为空
接下来,通过``if (enc.length() == 0)``判断编码方式是否为空,如果编码方式enc为空,则抛出异常,提示传递进来的编码方式为空,且不支持传递空编码方式。
    4. 解码该字符串
在decode()方法中,主要是使用三种方式来判断解码的字符串是否为需要解码的字符串:
- 转化" ":如果需要解码的字符串中有" ",则解码之后就变成" "。
- 转化"%xy":当发现"%xy"的情况时,将该字符串解析为十进制数字 v,再将数字v按照指定的编码方式enc转化为字符。
- 不进行转化:当最后一个字符不是" "或 "%xy"时,将其原样取出进行解码。
    5. 返回解码后的字符串
在解码完毕后,返回解码后的字符串。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。