前台通过 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);