Java HttpServletRequest UTF-8 编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16527576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:19:41  来源:igfitidea点击:

HttpServletRequest UTF-8 Encoding

javaencodingutf-8httprequest

提问by Carvallegro

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.

我想从请求中获取我的参数(带重音的字符),但它不起作用。我尝试使用用户,request.setCharacterEncoding( "UTF-8" )但它也不起作用。

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 ?

我知道这会URLDecoder.decode( request.getQueryString(), "UTF-8" )返回我的权利字符,但request.getParameterValues()不起作用!有没有人有想法?

采纳答案by VGR

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:

Paul 的建议似乎是最好的行动方案,但如果您要解决它,则根本不需要 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");

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.

更新:由于这获得了很多选票,我想强调 BalusC 的观点,这绝对不是解决方案;这充其量是一种解决方法。人们不应该这样做。

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.

我不知道究竟是什么导致了最初的问题,但我怀疑 URL 已经是 UTF-8 编码的,然后又是 UTF-8 编码的。

回答by Paul Vargas

If you are using Apache Tomcat, request.setCharacterEncoding("UTF-8")only works with POSTrequest.

如果您使用的是 Apache Tomcat,则request.setCharacterEncoding("UTF-8")仅适用于POST请求。

For GETrequest, you need add URIEncoding="UTF-8"on your <Connector>in server.xml.

对于GET请求,您需要添加URIEncoding="UTF-8"您的<Connector>in server.xml

See more in FAQ/CharacterEncoding - Apache Tomcat wiki space.

FAQ/CharacterEncoding - Apache Tomcat wiki 空间中查看更多信息。

回答by Carvallegro

Well, I found a way to resolve my problem, I guess that's not the better solution, but it does works..

好吧,我找到了解决问题的方法,我想这不是更好的解决方案,但它确实有效..

  String item = request.getParameterValues("param"); // H??l?¨ne
  item = URLEncoder.encode( model.getNameItem(), "ISO-8859-1" ); // H%C3%A9l%C3%A8ne
  item = URLDecoder.decode( item, "UTF-8" ); // and finally : Hélène

If it can help anyone else, I'm glad to help :)

如果它可以帮助其他人,我很乐意帮助:)

PS : Paul, I didn't answer you earlier but I already did those things. Thanks anyway for your help.

PS:保罗,我之前没有回答你,但我已经做了这些事情。无论如何感谢您的帮助。