Java URLDecoder 在与包含 % 的字符串一起使用时抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10590080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:42:49  来源:igfitidea点击:

Java URLDecoder throws exception when used with a string containing a %

javaurldecoding

提问by Daniel Rotter

I have a problem with the URLDecoder of Java. I am escaping a String in JavaScript, and send it to a java servlet. Then I decode the escaped String with the following line:

我对 Java 的 URLDecoder 有问题。我在 JavaScript 中转义一个字符串,并将它发送到一个 java servlet。然后我使用以下行解码转义的字符串:

URLDecoder.decode(request.getParameter("text"), "UTF-8");

This works fine for every special characters I have tried, the only one making problems is the '%'. Everytime I use this character in the string, I get the following exception:

这适用于我尝试过的每个特殊字符,唯一的问题是“%”。每次在字符串中使用此字符时,都会出现以下异常:

java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern
    java.net.URLDecoder.decode(URLDecoder.java:187)
    at.fhv.students.rotter.ajax.count.CountServlet.doGet(CountServlet.java:31)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Is this a known bug? Or is it really my mistake?

这是一个已知的错误?还是真的是我的错?

回答by Christian Kuetbach

It is not a bug. You send a wrong encoded String. The %-sign has to be encoded as %25

这不是一个错误。您发送了错误的编码字符串。-%符号必须编码为%25

If you call request.getParameter(), I think you get a decoded String.

如果你调用 request.getParameter(),我想你会得到一个解码的字符串。

回答by ravindra nath

We had a similar issue in our angular application where we were encoding %sign once in client side code. When we received the value in servlet it was already decoded due to request.getParameter(). Since we already had URL decoder in our sever side code, decoding the %sign twice was causing a "URLDecoder: Incomplete trailing escape (%) pattern"exception. We figured out that we we should not encode and decode %as a value at all to get face this issue.

我们在 angular 应用程序中遇到了类似的问题,我们%在客户端代码中对符号进行了一次编码。当我们收到 servlet 中的值时,由于request.getParameter(). 由于我们的服务器端代码中已经有 URL 解码器,因此对%符号进行两次解码会导致"URLDecoder: Incomplete trailing escape (%) pattern"异常。我们发现我们根本不应该将编码和解码%作为一个值来解决这个问题。

回答by dubet

Even I faced similar issue and it was solved. Following is the example code you can simply run to reproduce and resolve this issue.

即使我遇到了类似的问题,它也得到了解决。以下是您可以简单地运行以重现和解决此问题的示例代码。

public class TestPercentage {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String transResult = "Se si utilizza DHCP%2C i valori validi sono S%C3%AC o No.%24%23%24%23%24%23%25NICyUSEWINS%25%24%23%24%23%24%23Se si utilizza WINS%2C i valori validi sono S%C3%AC o No.%24%23%24%23%24%23%25NODEFULL%25%24%23%24%23%24%23Nome completo del computer%24%23%24%23%24%23%25NODENAME%25%24%23%24%23%24%23I primi 8 caratteri del nome effettivo del computer%24%23%24%23%24%23%25NWCONTEXT%25%24%23%24%23%24%23Nome contesto NetWare%24%23%24%23%24%23%25NWSERVER%25%";
        String decode = null;
        try {
            decode = URLDecoder.decode(transResult, "UTF-8");
        } catch (UnsupportedEncodingException ue) {
            System.out.println("UnsupportedEncodingException ! = " + ue);
        } catch (IllegalArgumentException ile) {
            System.out.println("IllegalArgumentException ! = " + ile);
            if (transResult.endsWith("%")) {
                transResult = transResult.substring(0, transResult.lastIndexOf("%"));
                System.out.println("transResult2 = " + transResult);
                try {
                decode = URLDecoder.decode(transResult, "UTF-8");
                } catch (UnsupportedEncodingException ue2) {
                    System.out.println("UnsupportedEncodingException 2 = " + ue2);
                } catch (IllegalArgumentException ile2) {
                    System.out.println("IllegalArgumentException ! = " + ile2);
                }
            }
        }
        System.out.println("decode = " + decode);
    }

}

回答by Dipendra Shrestha

In order to get parameter I have written

为了获得参数,我写了

String requestURL=request.getQueryString(); 

so that It will give us parameters. From that we can use String.substring()to get prefered parameter in case of fix length or single parameter. Then

这样它就会给我们参数。String.substring()在固定长度或单个参数的情况下,我们可以使用它来获取首选参数。然后

String decodeValue = URLDecoder.decode(value,"UTF-8"); 

will get preferred string encoded % sign too.

也将获得首选字符串编码的 % 符号。