读取http请求而不会丢失数据

时间:2016-01-25 16:35:51

标签: java servlets servlet-3.0

有没有办法在不丢失数据的情况下读取请求的inputStream。

我正在尝试将处理它的请求的原始副本转换为字符串。但是一旦我从请求中读取输入流,输入流就会变为null,所以我以后无法从我的请求中获取参数。我尝试使用CustomHttpServletRequestWrapper但它没有用。以下是我的代码片段。

InboundHandler是我的处理程序类,它具有从我的servlet类调用的processRequest(HttpServletRequest request, HttpServletResponse response)方法。

public class InboundHandler {

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  try {
   CustomHttpServletRequestWrapper requestWrapper = new CustomHttpServletRequestWrapper(request);
   String body = getRequestBody(requestWrapper.getInputStream());

   String from = request.getParameter("from"); //which I'm getting null here
   // I also tried using 
   // String from = requestWrapper.getParameter("from"); // Even this did not work
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private String getRequestBody(InputStream inputStream) {

  StringBuilder stringBuilder = new StringBuilder();
  BufferedReader bufferedReader = null;
  try {
   if (inputStream != null) {
    bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    char[] charBuffer = new char[128];
    int bytesRead = -1;
    while ((bytesRead = bufferedReader.read(charBuffer)) != -1) {
     stringBuilder.append(charBuffer, 0, bytesRead);
    }
   }
  } catch (IOException ex) {
   ex.printStackTrace();
   //throw new AuthenticationException("Error reading the request payload", ex);
  } finally {
   if (bufferedReader != null) {
    try {
     bufferedReader.close();
    } catch (IOException iox) {
     // ignore
    }
   }
  }

  return stringBuilder.toString();
 }

 public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {

  private final String body;

  public CustomHttpServletRequestWrapper(HttpServletRequest request) {
   super(request);
   StringBuilder stringBuilder = new StringBuilder();
   BufferedReader bufferedReader = null;

   try {
    InputStream inputStream = request.getInputStream();

    if (inputStream != null) {
     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) {
    ex.printStackTrace();
    //log.error("Error reading the request body...");  
   } finally {
    if (bufferedReader != null) {
     try {
      bufferedReader.close();
     } catch (IOException ex) {
      ex.printStackTrace();
      //log("Error closing bufferedReader...");  
     }
    }
   }

   body = stringBuilder.toString();
  }

  @Override
  public ServletInputStream getInputStream() throws IOException {

   final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());

   ServletInputStream inputStream = new ServletInputStream() {
    public int read() throws IOException {
     return byteArrayInputStream.read();
    }
   };

   return inputStream;
  }
 }
}

1 个答案:

答案 0 :(得分:-1)

问:有没有办法在不丢失数据的情况下读取请求的inputStream?

<答>答:是的 - 只是&#34;保存&#34;你读什么&#34;!

正如Perdomoff正确地说的那样,&#34;请求并不意味着可以重复使用&#34;。一旦浇灌了花园,就不能将水倒回软管中。)

可以保存数据。 Perdomoff建议使用会话变量。

另一种选择可能是使用Servlet Filter。这是一个很好的教程: