码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot解决redirect参数中文乱码问题

时间:2018-09-22 16:02:35      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:new   utf-8   tac   中文   charset   exce   default   system   字符串转换   

代码如下:
&name=" + new URLEncoder().encode(user.getName(), Charset.defaultCharset()) ;
只需要将中文参数encode一下就ok了。

当然,知其然更要知其所以然。

URLEncoder和URLDecoder的作用如下:

1.URLEncoder.encode(String s, String enc) 
使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式 

URLDecoder.decode(String s, String enc) 
使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。 

2.发送的时候使用URLEncoder.encode编码,接收的时候使用URLDecoder.decode解码,都按指定的编码格式进行编码、解码,可以保证不会出现乱码

3.主要用来http get请求不能传输中文参数问题。http请求是不接受中文参数的。

这就需要发送方,将中文参数encode,接收方将参数decode,这样接收方就能收到准确的原始字符串了。

如:

技术分享图片
String testString = "abcdefghijk";
        try
        {
            String encoderString = URLEncoder.encode(testString, "utf-8");
            System.out.println(encoderString);
            String decodedString = URLDecoder.decode(encoderString, "utf-8");
            System.out.println(decodedString);
        } catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

SpringBoot解决redirect参数中文乱码问题

标签:new   utf-8   tac   中文   charset   exce   default   system   字符串转换   

原文地址:https://www.cnblogs.com/ergexy/p/9690204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!