欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

request.getInputStream() 只能读一次的解决方法

程序员文章站 2022-03-10 12:09:42
...
我们知道request.getInputStream()只能读一次,如果下一次读,就会报告Stream closed的错误。下面是解决方法。


InputStream input = request.getInputStream();

ByteArrayOutputStream bytebuffer= new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1 ) {
bytebuffer.write(buffer, 0, len);
}
bytebuffer.flush();

InputStream stream1 = new ByteArrayInputStream(bytebuffer.toByteArray());




输出request.getInputStream()内容的方法如下:


StringBuffer buffer = new StringBuffer();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line = "";
while ((line = reader.readLine()) != null)
buffer.append(line);
}

logger.info("接收xml数据:" + buffer);