“URIEncoding”和“URIEncoding”之间的区别是什么? Tomcat,编码过滤器和request.setCharacterEncoding [英] What's the difference between "URIEncoding" of Tomcat, Encoding Filter and request.setCharacterEncoding

查看:125
本文介绍了“URIEncoding”和“URIEncoding”之间的区别是什么? Tomcat,编码过滤器和request.setCharacterEncoding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决编码问题的方法可能有很多种:

There may be many ways to solve encoding problem:

设置URIEncoding = Tomcat的server.xml中的UTF-8,如 http://struts.apache.org/release/2.1.x/docs/how-to-support-utf-8-uriencoding-with-tomcat.html

Setting URIEncoding=UTF-8 in server.xml of Tomcat , like http://struts.apache.org/release/2.1.x/docs/how-to-support-utf-8-uriencoding-with-tomcat.html.

request.setCharacterEncoding(utf-8)

request.setCharacterEncoding( utf-8 )

今天,我遇到一个问题,即路径参数解码不好像

Today, I have a problem that path param is not decoded well like

@ResponseBody
@RequestMapping(value="/context/method/{key}",method=RequestMethod.GET,produces = "application/json;charset=utf-8")
public String method(@PathVariable String key){

    logger.info("key="+key+"------------");
}

我可以看到密钥被解码坏了!如果我从前端传递一个单词新浪,它将变为æ°æμª。我编写下面的代码来检查服务器是否使用ISO-8859-1对其进行解码:

I can see that the key is decoded bad! If I pass a word "新浪" from the front end, it will become "æ°æµª". I write the below code to examine if the server is decoding this with "ISO-8859-1":

public static void main(String args[]) throws UnsupportedEncodingException{
    String key="新浪";
    byte[] bytes=key.getBytes("UTF-8");
    String decode=new String(bytes,"ISO-8859-1");
    System.out.println(decode);
}

并且它输出相同的输出æ °æμª。确实,路径变量用ISO-8859-1解码。

And it comes out with the same output "æ°æµª". so indeed, the path variable is decoded with ISO-8859-1.

然后我尝试在我的 web.xml中添加一个过滤器解决此问题:

And then I try to add a filter to my web.xml to solve this problem:

  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
 </filter>

  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping> 

但是同样的乱码。

直到我在下面设置 server.xml

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"
           URIEncoding="UTF-8" useBodyEncodingForURI="true"   ----Here is Added
/>

即使我删除过滤器也适用于此。

And it works for this even I remove the filter.

但我仍然对编码问题感到困惑。此外,这只是GET方法,如果是POST方法,我猜解决方案可能会有所不同

But I am still very confusing about the encoding issue. And besides , this is only GET method, if it is POST method, I guess the solution will probably be different

有人可以解释我们应该采取什么样的差异编码解决方案出于什么问题?

Can anybody please explain that what difference encoding solution should we take for what kind of problem ?

谢谢!

推荐答案


  • CharacterEncodingFilter 配置请求正文的编码。也就是说,它会影响 POST 请求参数等的编码,但不会影响 GET 参数的编码

    • CharacterEncodingFilter configures encoding of request body. That is, it affects encoding of POST request parameters, etc, but doesn't affect encoding of GET parameters

      URIEncoding 用于指定URI的编码,因此会影响 GET parameters

      URIEncoding is used to specify encoding of the URI, therefore it affects GET parameters

      useBodyEncodingForURI =true告诉Tomcat使用配置的编码解码URI时请求正文。所以,据我所知,如果你设置 CharacterEncodingFilter useBodyEncodingForURI =true那么你不需要 URIEncoding

      useBodyEncodingForURI="true" tells Tomcat to use encoding configured for request body when decoding URIs. So, as far as I understand, if you set CharacterEncodingFilter and useBodyEncodingForURI="true" then you don't need URIEncoding.

      在实践中,你需要两件事解决参数编码可能出现的问题:

      In practice, you need to two things to solve possible problems with encoding of parameters:


      • CharacterEncodingFilter for POST requests

      • CharacterEncodingFilter for POST requests

      URIEncoding (或 useBodyEncodingForURI =true GET requests

      URIEncoding (or useBodyEncodingForURI="true") for GET requests

      这篇关于“URIEncoding”和“URIEncoding”之间的区别是什么? Tomcat,编码过滤器和request.setCharacterEncoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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