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. navicat - Mysql无法连接2. mysql - SELECT 多個資料表及多個欄位3. 【python小白】 问关于__new__方法的作用的问题4. python - celery beat 调度如何运行期间动态添加任务?5. PHP中的$this代表当前的类还是方法?6. python中merge后文件莫名变得非常大7. 级联对象 - mysql 级联查询问题8. php mail无法发送邮件9. python - 如何使用requests模块在一个post中提交含有多个相同名称的数据?10. thinkphp中Controller里面使用Request::File(’image’);

网公网安备