与包含%的字符串一起使用时,Java URLDecoder引发异常 [英] Java URLDecoder throws exception when used with a string containing a %

查看:82
本文介绍了与包含%的字符串一起使用时,Java URLDecoder引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java的URLDecoder有问题。我正在用JavaScript转义一个String,并将其发送到Java servlet。然后,我用以下行解码转义的String:

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:

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?

推荐答案

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

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

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

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

这篇关于与包含%的字符串一起使用时,Java URLDecoder引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆