国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

java - inputstream轉(zhuǎn)為byte數(shù)組 數(shù)組越界

瀏覽:77日期:2023-12-09 10:37:15

問題描述

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); }

}

網(wǎng)上都是這種處理方式 寫死有越界的可能性

不知道有沒有其他的處理方式

問題解答

回答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;

標簽: java
相關文章:
主站蜘蛛池模板: 安乡县| 县级市| 南木林县| 九江县| 泾源县| 中方县| 武山县| 荣成市| 中宁县| 陇西县| 双桥区| 阳高县| 嘉兴市| 礼泉县| 云林县| 壤塘县| 元氏县| 芒康县| 北辰区| 雅安市| 张掖市| 肃宁县| 静安区| 安平县| 财经| 科技| 蓬溪县| 保靖县| 黎城县| 若羌县| 红原县| 昔阳县| 陆丰市| 临沂市| 闸北区| 都昌县| 始兴县| 耿马| 怀远县| 太保市| 古田县|