java - inputstream转为byte数组 数组越界
本文介绍了java - inputstream转为byte数组 数组越界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
public static byte[] readInputStream(InputStream inStream) throws Exception {
try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}catch (Exception e){
e.printStackTrace();
throw new Exception(e);
}
}
网上都是这种处理方式 写死有越界的可能性
不知道有没有其他的处理方式
解决方案
最好的方法是用Apache commons IO的IOUtils.toByteArray(inputStream),一行代码解决。
这篇关于java - inputstream转为byte数组 数组越界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文