Java 和 JS/AS3 之间 URL 解码/编码 UTF-8 的差异(bug!?)
我在对 Java 中使用 Javascript 或 Actionscript 3 编码的 UTF-8 字符串进行 URL 解码时遇到问题。我设置了一个测试用例,如下所示:
The string in question is Produktgröße
当我使用 JS/AS3 编码时,我得到以下字符串:
escape('Produktgröße')
Produktgr%F6%DFe
当我用 JS 取消转义时,我没有得到任何变化
unescape('Produktgr%F6%DFe')
Produktgr%F6%DFe
因此,我假设 JS 没有对字符串进行编码 适当地??
以下 JSP 产生此输出
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%=(URLDecoder.decode("Produktgr%F6%DFe","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße"))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße")))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße"),"UTF-8"))%><br/>
Produktgr?e
Produktgr%C3%B6%C3%9Fe
Produktgr%C3%B6%C3%9Fe
Produktgröße
Produktgröße
知道为什么我与语言存在这种差异以及为什么 JS/AS3 不表现正如我所期望的那样?
谢谢。
I am having an issue URL decoding a UTF-8 string in Java that is encoded either with Javascript or Actionscript 3. I've set up a test case as follows:
The string in question is Produktgröße
When I encode with JS/AS3 I get the following string:
escape('Produktgröße')
Produktgr%F6%DFe
When I unescape this with JS I get no change
unescape('Produktgr%F6%DFe')
Produktgr%F6%DFe
So, by this I assume that JS isn't encoding the string properly??
The following JSP produces this outupt
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%=(URLDecoder.decode("Produktgr%F6%DFe","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße"))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße")))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße"),"UTF-8"))%><br/>
Produktgr?e
Produktgr%C3%B6%C3%9Fe
Produktgr%C3%B6%C3%9Fe
Produktgröße
Produktgröße
Any idea why I'm having this disparity with the languages and why JS/AS3 isn't behaving as I expect it to?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
escape 是一个已弃用的函数,并且不能正确编码 Unicode 字符。使用 encodeURI 或 encodeURIComponent,后者可能是最适合您需求的方法。
escape is a deprecated function and does not correctly encode Unicode characters. Use encodeURI or encodeURIComponent, the latter probably being the method most suitable for your needs.
Javascript 是使用 Latin-1 字符集对字符串进行 URL 编码。 Java 使用 UTF-8 对它进行 URL 编码。
URL 编码实际上只是替换它无法识别的字符/字节。例如,即使您坚持使用 ASCII 字符,
(
也会被编码为%28
。当您开始使用非 ASCII 时,您还会遇到字符集的额外问题字符(任何超过 7 位的字符)。Javascript is URL encoding your string using Latin-1 charset. Java is URL encoding it using UTF-8.
The URL encoding is really just replacing the characters/bytes that it doesn't recognise. For example, even if you were to stick with ASCII characters,
(
would be encoded as%28
. You have the additional problem of character sets when you start using non-ASCII characters (any thing longer than 7 bits).我已经在这个问题上挣扎了好几个小时了......
我的问题是 JQuery Ajax 调用,例如:
“name”是一个包含 Jérôme-Serrano 等特殊字符的字符串
由于某些原因,JS/JQuery 编码这些特殊字符的方式不兼容,我无法在 Java 后端对其进行解码...
解决方案是:
var econded =encodeURIComponent(name); 在 JS 端进行编码;
String Decoded = 在 Java 端进行解码java.net.URLDecoder.decode(econded ,"UTF-8");
一些参考文献:
http://www.programering.com/a/MjN2ADOwATg.html
http: //www.theerrormessage.com/2013/10/weird-characters-transmission-to-and-from-server-through-jquery-ajax-call/
I have been struggling with this problem for hours on end...
My problem was a JQuery Ajax call like:
'name' is a String which contains special characters like Jérôme-Serrano
For some reasons the way JS/JQuery was encoding these kind of special characters was incompatible and I couldn't decode it on Java BackEnd...
The solution was:
var econded = encodeURIComponent(name);
String decoded = java.net.URLDecoder.decode(econded ,"UTF-8");
some refetences:
http://www.programering.com/a/MjN2ADOwATg.html
http://www.theerrormessage.com/2013/10/weird-characters-transmitted-to-and-from-server-through-jquery-ajax-call/