Transform ServletInputStream to String
I am trying to get the bo开发者_StackOverflow社区dy of a HttpServletRequest in a String. What's the best elegant way to do so?
Using Apache Commons IO:
String requestStr = IOUtils.toString(request.getInputStream());
Other way, using Guava:
ByteSource.wrap(ByteStreams.toByteArray(request.getInputStream()))
.asCharSource(Charsets.UTF_8).read()
See also:
- http://www.baeldung.com/java-convert-inputstream-to-reader
精彩评论