重复读取request中的inputstream

xiaoxiao2021-02-28  4

public RequestWrapper(HttpServletRequest request) { int contentLength = request.getContentLength(); StringBuilder stringBuilder = new StringBuilder(); InputStream inputStream = null; try { inputStream = request.getInputStream(); } catch (IOException e) { } if (inputStream != null) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { char[] charBuffer = new char[contentLength]; int bytesRead; while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { stringBuilder.append(charBuffer, 0, bytesRead); } } catch (IOException e) { } } else { stringBuilder.append(""); } body = stringBuilder.toString(); } public ServletInputStream getInputStream() throws IOException { return new ServletInputStreamWrapper(new ByteArrayInputStream(body.getBytes())); } }
转载请注明原文地址: https://www.6miu.com/read-2050086.html

最新回复(0)