一、项目配置:

  1. Spring 4.4.1-RELEASE
  2. Jetty 9.3.5
  3. JDK 1.8
  4. Servlet 3.1.0
  5. web.xml文件中没有配置编解码Filter

二、实际遇到的问题:
客户端(比如java)发送post请求访问接口,数据放在body里面,每个参数utf-8编码。
从body里面取出的中文参数是乱码。


下面是发送请求的代码和服务端接收请求的代码。

  • 客户端代码。
    这是一个真实的第三方访问API的案例,这段代码请求到PHP系统正常,请求到java系统就会出现乱码。
    但是中文参数放到URL中解码正常,放到请求体中就是乱码。
    通过httpclient4.1发送Post请求如下:

    public static void postData(String sign, String timestamp) {    // 创建默认的httpClient实例.
    CloseableHttpClient httpclient=null;
    String result="";
    try {
    httpclient = HttpClients.createDefault();
    String url = "http://example/api/entry";
    HttpPost httpPost = new HttpPost(url); //设置请求和传输超时时间
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(6000).build();
    httpPost.setConfig(requestConfig);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("app_id", new StringBody("c5eb3ba8c0e7326559", Charset.forName("utf-8")));
    entity.addPart("method", new StringBody("kdt.item.add", Charset.forName("utf-8")));
    entity.addPart("timestamp", new StringBody(timestamp));
    entity.addPart("format", new StringBody("json", Charset.forName("utf-8")));
    entity.addPart("v", new StringBody("1.0", Charset.forName("utf-8")));
    entity.addPart("sign", new StringBody(sign, Charset.forName("utf-8")));
    entity.addPart("sign_method", new StringBody("md5", Charset.forName("utf-8")));
    entity.addPart("cid", new StringBody("5000000", Charset.forName("utf-8")));
    entity.addPart("tag_ids", new StringBody("0", Charset.forName("utf-8")));
    entity.addPart("price", new StringBody("0.01", Charset.forName("utf-8")));
    entity.addPart("title", new StringBody("测试", Charset.forName("utf-8")));
    entity.addPart("desc", new StringBody("test1", Charset.forName("utf-8"))); //是否是虚拟商品。0为否,1为是。目前不支持虚拟商品
    entity.addPart("is_virtual", new StringBody("0", Charset.forName("utf-8")));
    entity.addPart("post_fee", new StringBody("0.0", Charset.forName("utf-8"))); //Sku的属性串。格式:pText:vText;pText:vText,多个sku之间用逗号分隔,如:颜色:黄色;尺寸:M,颜色:黄色;尺寸:S。pText和vText文本中不可以存在冒号和分号以及逗号
    entity.addPart("sku_properties", new StringBody("color:white", Charset.forName("utf-8")));
    entity.addPart("sku_quantities", new StringBody("998,999", Charset.forName("utf-8")));
    entity.addPart("sku_prices", new StringBody("0.01,0.02", Charset.forName("utf-8")));
    entity.addPart("sku_outer_ids", new StringBody("null,null", Charset.forName("utf-8"))); //该商品的外部购买地址。当用户购买环境不支持微信或微博支付时会跳转到此地址
    entity.addPart("buy_url", new StringBody("http://img.cdn.sb.hongware.com/1461836641703511.gif", Charset.forName("utf-8")));
    entity.addPart("quantity", new StringBody("1998", Charset.forName("utf-8"))); //宝贝修改的时候需要这个参数
    httpPost.setEntity(entity);
    CloseableHttpResponse response = httpclient.execute(httpPost);
    try {
    HttpEntity httpEntity = response.getEntity();
    System.out.println(httpEntity.getContent());
    InputStream content = httpEntity.getContent();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
    String line;
    while ( (line=bufferedReader.readLine()) != null) {
    System.out.println(line);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    response.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    httpclient.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
  • 服务端代码如下
    为了简化演示,我把参数提取代码Map<String, String[]> parameterMap = request.getParameterMap()冗余在这个入口函数中,以便说明问题:

    @RequestMapping(value = "/api/entry", produces = "application/json;charset=utf-8")
    public DeferredResult<Object> sign(HttpServletRequest request,HttpServletResponse response) {
    DeferredResult<Object> deferredResult = new DeferredResult<>();
    Map<String, String[]> parameterMap = request.getParameterMap();
    String method = request.getParameter("method");
    if (StringUtils.isEmpty(method)) {
    ResponseEntity<String> responseEntity = new ResponseEntity<String>( String.format(Constants.ERROR_RESPONSE, 50000, "service or method is null"), HttpStatus.valueOf(200));
    deferredResult.setResult(responseEntity);
    return deferredResult;
    }
    int lastIndex = method.lastIndexOf(".");
    String service = method.substring(0, lastIndex);
    method = method.substring(lastIndex + 1);
    event.setService(service);
    event.setMethod(method);
    event.setResult(deferredResult);
    proxy.doAction(request,response,event);
    return deferredResult;
    }

服务端通过Map<String, String[]> parameterMap = request.getParameterMap()取出所有参数,传进来title参数是乱码!!

三、根本原因
Servlet 3.0规范中有关请求数据编码的解释如下:

当前很多浏览器并不发送带Content-Type头部的字符编码标识符,它会把字符编码的决定留在读取HTTP请求的时候。如果客户端没有指明编码,容器用来创建请求读和解析POST数据的默认编码必须是"ISO-8859-1"。然而,为了提示开发者客户端没有成功发送一个字符编码,容器中getCharacterEncoding方法会返回null。
如果客户端没有设置字符编码,并且请求数据使用了不同编码而不是上述的默认编码,程序将会出现中断。为了纠正这种状态,一个新的方法setCharacterEncoding(String enc) 被添加到ServletRequest接口。开发者调用这个方法能重写容器提供的字符编码。这个方法必须在解析request中任何post数据或者读任何输入之前调用。一旦数据已经被读取,调用这个方法不会影响它的编码。

另外一种相同的解释:

网络上同样含义的解释

四、3种解决方法

  1. 在web.xml中配置编解码Filter

     <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>
    <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    关于这段配置需要强调两点:

    • web.xml中,这段配置要放在所有filter的最前面,否则会不生效,根本原因请见上述第三点的解释。
    • 两个初始化参数的作用,其实看这个Filter的源码就一目了然,这两个参数是用来决定是否要设置request和response中的编码。源码很简洁:
       public class CharacterEncodingFilter extends OncePerRequestFilter {
      private String encoding;
      private boolean forceEncoding = false;
      public CharacterEncodingFilter() { }
      public void setEncoding(String encoding) {
      this.encoding = encoding;
      }
      public void setForceEncoding(boolean forceEncoding) {
      this.forceEncoding = forceEncoding;
      }
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
      if(this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
      request.setCharacterEncoding(this.encoding);
      if(this.forceEncoding) {
      response.setCharacterEncoding(this.encoding);
      }
      }
      filterChain.doFilter(request, response);
      }
      }
  2. 设置Content-Type
    如果post请求方式是x-www-form-urlencoded,那么设置如下:
    Content-Type=application/x-www-form-urlencoded;charset=utf-8
    这样通过request对象取body体里面的中文是正常的。
    这种方式有一点需要注意: 如果请求方式是multipart/form-data,如上设置会导致request取不到参数。Content-Type要与传递数据匹配(本文data)
  3. 手动编解码
    比如参数title="测试",这样取出来就是"测试"。
     String str = new String(request.getParameter("title").getBytes("iso-8859-1"), "utf-8");

综上所有,最优雅的方式是第一种解决方案--通过框架的Filter去处理。
你仅专注于业务代码就好。

参考资料

    1. ajax post data获取不到数据
    2. Servlet 3.0规范
    3. HTTP Content-Type常用对照表
    4. Spring官网--Consumable Media Types章节
    5. ISO-8859-1
    6. ISO-8859-1为何能显示中文
    7. 字符编码
    8. Media Type

Spring MVC的Post请求参数中文乱码的原因&处理的更多相关文章

  1. spring mvc 文件下载 get请求解决中文乱码问题

    方案简写,自己或有些基础的可以看懂,因为没时间写的那么详细 方案1 spring mvc解决get请求中文乱码问题, 在tamcat中server.xml文件 URIEncoding="UT ...

  2. java web项目get,post请求参数中文乱码解决

    [转载]原文地址:https://www.cnblogs.com/tom-plus/p/6392279.html 在开发过程中,有时候会碰到get,post请求参数中文乱码. 原因: Http请求传输 ...

  3. 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)

    前述:      在写这篇笔记之前,对笔记中的设计模式进行介绍:      本篇笔记中将要使用到的设计模式是:装饰(包装)设计模式           (1)装饰(包装)设计模式口诀:         ...

  4. 详解get请求和post请求参数中文乱码的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  5. get请求和post请求参数中文乱码的解决办法

    get请求参数中文乱码的解决办法 在tomcat的server.xml里的Connector加个URIEncoding="UTF-8",把 <Connector connec ...

  6. 解决jmeter 请求参数中文乱码

    今天在用jmeter 写脚本时发现查看结果树request post请求中文参数值是乱码,故记录下解决过程. 解决过程如下: 1.修改本地配置文件 因为此处的数据,还没有发送出去,所以,肯定是这个变量 ...

  7. JSP中解决获取请求参数中文乱码问题

    分两种情况: 1.获取访问请求参数时乱码 解决方法:构造一个新的String String user = new String(request.getParameter("user" ...

  8. SpringMVC 接收表单数据、数据绑定、解决请求参数中文乱码

    接收表单数据有3种方式. 1.使用简单类型接收表单数据(绑定简单数据类型) 表单: <form action="${pageContext.request.contextPath}/u ...

  9. Spring MVC 结合Velocity视图出现中文乱码的解决方案

    编码问题一直是个很令人头疼的事,这几天搭了一个Spring MVC+VTL的web框架,发现中文乱码了,这里记录一种解决乱码的方案. 开发环境为eclipse,首先,检查Window->pref ...

随机推荐

  1. 从源码角度一步一步来修改PreferenceActivity界面

         PreferenceActivity给我们封装好了一个数据存储对象,我们只需要在xml文件中写上控件即可完成简单的设置界面.但是系统提供的设置界面十分的简陋,要想做的好看必须要自己来进行修改 ...

  2. Orchard模块开发全接触8:改造后台

    后台默认提供了 Content 的管理,但是,所有的内容类型揉杂在一起,而我们需要更深度的定制一些功能,比如,我们只想管理订单,又比如,我们需要对注册用户进行管理.本篇的内容包括: 1:自定义 adm ...

  3. C# 根据注册表获取当前用户的常用目录整理

    1.使用C#获取当前程序或解决方案的路径 2.使用C#获取当前登录用户的相关目录 3.也可以获取当前系统通用目录 4.获取Windows系统的目录,从注册表中获取. 一.当前用户的目录,HKEY_Cu ...

  4. 反向解析与PTR(Pointer Record)

    PTR记录,是电子邮件系统中的邮件交换记录的一种:另一种邮件交换记录是A记录(在IPv4协议中)或AAAA记录(在IPv6协议中).PTR记录常被用于反向地址解析. PTR记录    Pointer ...

  5. DatabaseMirroring搭建

    1.    概述 数据库镜像维护一个数据库的两个副本,这两个副本必须驻留在不同的 SQL Server 数据库引擎 服务器实例上.通常,这些服务器实例驻留在不同位置的计算机上.启动数据库上的数据库镜像 ...

  6. JS 判断上传 文件 大小

    随着HTML5 的发展,我们可以用file控件的size属性来获取客户端 上传文件的大小,但是 我今天测试 发现IE10支持,IE11的某个版本不支持, 于是就借用img控件来加载一此, 以此来获取文 ...

  7. 使用Vue.js实现列表选中效果

     实际项目中,我们会遇到很多类似的需求,一个列表,需要点击其中一条高亮显示.熟悉JQuery的同学说这个太简单了.可以给这个选中的element设置一个active的class.配合Css样式,让ac ...

  8. JQuery实现可编辑的表格

    点击表格后可直接编辑,回车或鼠标点击页面其他地方后编辑生效,按Esc可取消编辑 第一种单击表格可以编辑的方法 //相当于在页面中的 body标签加上onload事件$(function() {    ...

  9. Eclipse技术: 项目文件中过滤.o文件

    1. 右建项目 -> Properties. 2. 增加过滤规则

  10. 4444: [Scoi2015]国旗计划|贪心|倍增

    由于没有区间被其它区间包括这个条件,也就是假设li<lj那么一定满足ri<rj,就能够贪心搞一搞了. 假如区间[l,r]都已经被覆盖,那么能够继续找一个li在[l,r]范围内的最大的一个, ...