How to read a ServletInputStream more than once when you don't have control of the code that reads it a second time

前端 未结 2 2043
长发绾君心
长发绾君心 2021-01-20 16:02

I have a ServletInputStream that I need to read more than once (the code that reads it the second time is in an API I don\'t control). When using IOUtils.

2条回答
  •  时光取名叫无心
    2021-01-20 16:49

    If you want to examine or consume part or all of the request body before handing off the request to this other API method, then you probably cannot do that at the level of a user of the Servlet API. Instead, you need to work a little lower, consuming the Servlet API on the one hand, but also serving it to the other API.

    Specifically, you can preserve the data read from the request's input stream by whatever means you choose, and provide the same to the other API by wrapping the HttpServletRequest in an implementation that mostly delegates to the wrapped request object, but whose getInputStream() method provides a stream from which the whole request body can be read.

提交回复
热议问题