有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java URI#getQuery()无法解码非法字符

为什么

new URI(
    null,
    null,
    "/monitor/123456/data",
    "filterParams=[{\"id\":12,\"type\":\"dimension\"}]",
    null).getQuery()

返回

filterParams=[%7B%22id%22:12,%22type%22:%22dimension%22%7D]

而不是

"filterParams=[{\"id\":12,\"type\":\"dimension\"}]"

根据URI#getQuery()文件

The string returned by this method is equal to that returned by the #getRawQuery() method except that all sequences of escaped octets are decoded.

是JDK bug还是我在这里缺少一些理解


共 (1) 个答案

  1. # 1 楼答案

    根据私有方法URI上面的注释。解码它不是一个bug,而是一个功能:)

    /*
    Evaluates all escapes in s, applying UTF-8 decoding if needed.  Assumes
    that escapes are well-formed syntactically, i.e., of the form %XX.  If a
    sequence of escaped octets is not valid UTF-8 then the erroneous octets
    are replaced with '\uFFFD'.
    Exception: any "%" found between "[]" is left alone. It is an IPv6  literal with a scope_id
    */
    private static String decode(String s) {...}