您的位置:首页 > 其它

关于inputStream 对象重复使用的解决方法

2017-04-24 11:52 453 查看
/**
* 读取输入流数据
* //此方法是用于缓存H5网络请求数据,解决inputStream对象不能重复复用的问题
*/
public static byte[] streamToData(InputStream uristream) {
ByteArrayOutputStream outStream = null;
try {
outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = uristream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
return outStream.toByteArray();
} catch (Exception e) {
return null;
} finally {
try {
if(uristream !=null) {
uristream.close();
}
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}
byte[] data = FileUtils.streamToData(inputstream);//把需要复用的inputStream保存为data
InputStream in1 = new ByteArrayInputStream(data)
InputStream in2 = new ByteArrayInputStream(data)

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐