本文介绍了Java和JS / AS3之间的URL解码/编码UTF-8的差异(bug !?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个问题的URL解码用Java或ActionScript 3编码的Java中的UTF-8字符串。我已经设置了一个测试用例如下:

有问题的字符串是Produktgröße



当我用JS / AS3进行编码时,我得到以下字符串:

  escape('Produktgröße')

Produktgr%F6% DFe



当我用JS解密时,我没有改变

  unescape('Produktgr%F6%DFe')

Produktgr%F6%DFe



所以,我假设JS不正确地编码字符串?



以下JSP产生这个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



产品编号%C3%B6%C3%9Fe



Produktgröße



Produktgröße



任何想法为什么我有这种差异的语言,为什么JS / AS3不像我期待它?



谢谢。

解决方案

是不推荐使用的函数,不能正确编码Unicode字符。使用或,后者可能是最适合您需要的方法。


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.

解决方案

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.

这篇关于Java和JS / AS3之间的URL解码/编码UTF-8的差异(bug !?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:04