后台获取requestbody中数据时出现中文乱码的解决方法
作者:互联网
前台通过 post 提交数据(utf-8编码),后台读取到requestbody 中文出现乱码
使用 request.setCharacterEncoding(“UTF-8”); 方式无效,中文依旧乱码
最后通过URLDecoder解码解决:
result = java.net.URLDecoder.decode(result, “UTF-8”);
代码如下:
StringBuilder buffer = new StringBuilder("");
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
String line = null;
while((line = reader.readLine()) != null){
buffer.append(line);
}
String result = buffer.toString();
// 使用URLDecoder解码,解决中文乱码
result = java.net.URLDecoder.decode(result, "UTF-8");
System.out.println(result);
标签:requestbody,UTF,乱码,result,后台,new,line,URLDecoder 来源: https://blog.csdn.net/qq_36476631/article/details/116143479