//获取客户端InputStream对象
InputStream input= requset.getInputStream();
//将InputStream对象转换成ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1 ) {
byteArrayOutputStream.write(buffer, 0, len);
}
byteArrayOutputStream.flush();
//将byteArrayOutputStream可转换成多个InputStream对象,达到多次读取InputStream效果
InputStream inputStreamA = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
InputStream inputStreamB = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
//将InputStream转换成字符串
BufferedReader br = new BufferedReader(new InputStreamReader(inputStreamB,"UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
InputStream流的二次读取
最新推荐文章于 2024-09-25 14:30:28 发布