我正在解析维基百科上的一些图片链接。我在 http://en.wikipedia.org/wiki/Special:Export/Diego_Forl%C3%A1n 上看到了这个
当我使用已弃用的 URLEncoder.encode 时,我可以正确编码重音字符,但是当我指定“UTF-8”参数时,它会失败。维基百科上的文本是 utf8 AFAIK。
Diego+Forl%C3%A1n+vs+the+Netherlands.jpg 是正确的,而 Diego+Forl%E2%88%9A%C2%B0n+vs+the+Netherlands.jpg 是错误的。
scala> first
res24: String = Diego Forlán vs the Netherlands.jpg
scala> java.net.URLEncoder.encode(first, "UTF-8")
res25: java.lang.String = Diego+Forl%E2%88%9A%C2%B0n+vs+the+Netherlands.jpg
scala> java.net.URLEncoder.encode(first)
<console>:33: warning: method encode in object URLEncoder is deprecated: see corresponding Javadoc for more information.
java.net.URLEncoder.encode(first)
^
res26: java.lang.String = Diego+Forl%C3%A1n+vs+the+Netherlands.jpg
最佳答案
我猜 first
已经损坏,并且只是由于控制台配置隐藏的转码错误才正确呈现。
您可以通过在字符串中发出 UTF-16 代码单元来确认这一点:
for(c<-first.toCharArray()){print("\\u%04x".format(c.toInt))}
可能有更优雅的写法。
如果代码点编码正确,它将是:
U+00e1 á \u00e1
我希望某处使用 MacRoman 解码器对 UTF-8 编码数据进行解码。
codepoint glyph escaped x-MacRoman info
=======================================================================
U+221a √ \u221a c3, MATHEMATICAL_OPERATORS, MATH_SYMBOL
U+00b0 ° \u00b0 a1, LATIN_1_SUPPLEMENT, OTHER_SYMBOL
关于java - 为什么默认弃用的 java.net.URLEncoder.encode 有效,但在我指定字符集时却无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8190971/