问题描述
在调用 http POST ,获取请求体中的内容时,如果出现中文,在读取的时候,可能会引起“中文乱码”的现象。
如下读取方式 会 引起中文乱码
public XssRequestWrapper(HttpServletRequest request) throws IOException { super(request); this.request = request; StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try { InputStream inputStream = request.getInputStream(); if (inputStream != null) { // 如下方式读取 InputStream 流,会引发中文乱码问题 bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); char[] charBuffer = new char[128]; int bytesRead = -1; while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { stringBuilder.append(charBuffer, 0, bytesRead); } } else { stringBuilder.append(""); } } catch (IOException ex) { throw ex; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ex) { throw ex; } } } System.out.println("stringBuilder: " + stringBuilder.toString()); }
问题原因
在不设置编码的情况下,InputStreamReader 默认的读取编码是 ASCII 编码,中文会转成乱码
解决方案
在 InputStreamReader 读取时,需要指定读取的编码格式为 UTF_8 编码
如下读取方式 不会 引起中文乱码
public XssRequestWrapper(HttpServletRequest request) throws IOException { super(request); this.request = request; StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try { InputStream inputStream = request.getInputStream(); if (inputStream != null) { // 此处需要将编码格式设置为UTF_8,解决 InputStream 流读取时的中文乱码问题 bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); char[] charBuffer = new char[128]; int bytesRead = -1; while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { stringBuilder.append(charBuffer, 0, bytesRead); } } else { stringBuilder.append(""); } } catch (IOException ex) { throw ex; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ex) { throw ex; } } } System.out.println("stringBuilder: " + stringBuilder.toString()); }