使用 Java 获取服务器的编码
是否可以以编程方式获取 Tomcat 所使用的编码方案来解码 GET 参数?
我知道您可以强制 Tomcat 使用特定编码,但这是在我无法控制 Tomcat 设置时使用的。我正在考虑通过自己解析查询字符串来获得正确的编码,或者尝试使用 Tomcat 所做的任何解码来重新编码该值,然后使用 UTF-8 对其进行解码以获得正确的字符串。
Is it possible to programmatically get the encoding scheme used by, say Tomcat, to decode a GET parameter?
I know you can force Tomcat to use specific encodings, but this is for when i can't control the Tomcat settings. I'm thinking of getting the correct encoding by either parsing the query string myself, or trying to re-encode the value using whatever Tomcat did to decode it, and then decoding it with UTF-8 to get the correct string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 http://www.w3schools.com/tags/ref_urlencode.asp 的 URL编码是 ASCII,特殊字符的规则可以很容易地颠倒。 GET 中的参数是 URL 编码的。
如果您正在谈论 GET 本身的值,那么您的问题实际上没有意义(或者我误解了您),因为您将在 Java servlet 中获得一个字符串。该参数已经被解码。您无需担心编码问题。
From http://www.w3schools.com/tags/ref_urlencode.asp, the URL encoding is ASCII and the rules for special characters can easily be reversed. Parameters in GET are URL encoded.
If you are talking about the value in GET itself, then, your question does not really makes sense (or I misunderstand you), since you will obtain a string in your Java servlet. The parameter will already be decoded. You don't need to worry about the encoding.
您始终可以尝试执行
getCharacterEncoding
。但有一个保证。如果server.xml
中的 URIEncoding 属性未更改,您始终可以采用 ISO-8859-1。如果使用getParameter
获取字符串,则检索原始字节并使用 UTF-8 编码构造一个新字符串。 URL 始终转义为 UTF-8 文本。You can always try to perform
getCharacterEncoding
. There is though one guarantee. If the URIEncoding attribute has not been altered in theserver.xml
you can always assume ISO-8859-1. If you get the string withgetParameter
, retrieve the raw bytes and construct a new string with UTF-8 encoding. URLs are always escaped as UTF-8 text.