HttpServletRequest UTF-8编码 [英] HttpServletRequest UTF-8 Encoding
问题描述
这是事情:我想从请求(字符与重音符号)获取我的参数,但它不工作。我尝试使用用户
request.setCharacterEncoding(UTF-8)
,但它也不起作用。 我知道 URLDecoder.decode(request.getQueryString(),UTF-8)
返回我的权限字符,但 request.getParameterValues()
不行!
有没有人有想法?
谢谢
p>保罗的建议似乎是最好的行动方案,但如果你要解决这个问题,你根本不需要URLEncoder或URLDecoder:
String item = request.getParameter(param);
byte [] bytes = item.getBytes(StandardCharsets.ISO_8859_1);
item = new String(bytes,StandardCharsets.UTF_8);
// Java 6:
// byte [] bytes = item.getBytes(ISO-8859-1);
// item = new String(bytes,UTF-8);
更新:由于这是获得了很多投票,我想压力BalusC的观点,这绝对不是解决办法;这是最好的解决方法。人们不应该这样做。
我不知道是什么原因造成的,但我怀疑URL已经是UTF-8编码,然后是UTF -8再次编码。
I saw a lot of question like mine but not a single answer works...
Here's the thing : I want to get my parameters from the request (characters with accents) but it doesn't work. I tried to user request.setCharacterEncoding( "UTF-8" )
but it didn't work either.
I know that URLDecoder.decode( request.getQueryString(), "UTF-8" )
returns me the rights characters but request.getParameterValues()
doesn't work !
Does anyone have an idea ?
Thank you
Paul's suggestion seems like the best course of action, but if you're going to work around it, you don't need URLEncoder or URLDecoder at all:
String item = request.getParameter("param");
byte[] bytes = item.getBytes(StandardCharsets.ISO_8859_1);
item = new String(bytes, StandardCharsets.UTF_8);
// Java 6:
// byte[] bytes = item.getBytes("ISO-8859-1");
// item = new String(bytes, "UTF-8");
Update: Since this is getting a lot of votes, I want to stress BalusC's point that this definitely is not a solution; it is a workaround at best. People should not be doing this.
I don't know exactly what caused the original issue, but I suspect the URL was already UTF-8 encoded, and then was UTF-8 encoded again.
这篇关于HttpServletRequest UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!