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); }
}
网上都是这种处理方式 写死有越界的可能性
不知道有没有其他的处理方式
问题解答
回答1:最好的方法是用Apache commons IO的IOUtils.toByteArray(inputStream),一行代码解决。
回答2:int count = 0;while (count == 0) { count = inStream.available();}byte[] b = new byte[count];inStream.read(b);return b;
相关文章:
1. docker安装后出现Cannot connect to the Docker daemon.2. docker镜像push报错3. nignx - docker内nginx 80端口被占用4. docker-compose中volumes的问题5. php - Redis监控工具,大家有推荐的吗?6. javascript - 小程序如何点击获取下标7. spring - 关于关键字查询中遇到的问题:sql语句写到java中去的问题8. $fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题9. URL访问有问题啊10. 这一年从入门到认识PHP的过程

网公网安备