URL解码Java中的日语字符等 [英] URL decoding Japanese characters etc. in Java

查看:93
本文介绍了URL解码Java中的日语字符等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Servlet,它接收一些POST数据.因为此数据是x-www-form-urlencoded的,所以诸如サbonテン之类的字符串将被编码为サボテン.

I have a servlet that receives some POST data. Because this data is x-www-form-urlencoded, a string such as サボテン would be encoded to サボテン.

我如何将该字符串解编码回正确的字符?我已经尝试过使用URLDecoder.decode("encoded string", "UTF-8");,但没有任何区别.

How would I unencode this string back to the correct characters? I have tried using URLDecoder.decode("encoded string", "UTF-8"); but it doesn't make a difference.

我想对其进行取消编码的原因是,在我将这些数据显示在网页上之前,我先进行了&和并且此刻,它正在转义编码字符串中的& s,因此字符无法正确显示.

The reason I would like to unencode them, is because, before I display this data on a webpage, I escape & to & and at the moment, it is escaping the &s in the encoded string so the characters are not showing up properly.

推荐答案

不是的那些 URL编码.看起来像%E3%82%B5%E3%83%9C%E3%83%86%E3%83%B3.这些是十进制HTML/XML实体.要转义HTML/XML实体,请使用 Apache Commons Lang

Those are not URL encodings. It would have looked like %E3%82%B5%E3%83%9C%E3%83%86%E3%83%B3. Those are decimal HTML/XML entities. To unescape HTML/XML entities, use Apache Commons Lang StringEscapeUtils.

更新:当响应编码不是UTF-8时,您将得到问号.如果您使用的是JSP,只需将以下行添加到页面顶部:

Update as per the comments: you will get question marks when the response encoding is not UTF-8. If you're using JSP, just add the following line to top of the page:

<%@ page pageEncoding="UTF-8" %>

有关更多中途解决方案的详细信息,请参见本文.我宁愿一味地使用UTF8,也不愿意使用regexp,因为regexp并不会让您为统治世界做好准备.

See for more detail the solutions about halfway this article. I would prefer using-UTF8-all-the-way above fiddling with regexps since regexps doesn't prepare you for world domination.

这篇关于URL解码Java中的日语字符等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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