Spring MVC的Post请求参数中文乱码的原因&处理

一、项目配置

  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);
    }
    }
  1. 设置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)
  2. 手动编解码
    比如参数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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容