久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

問(wèn)題描述

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)上都是這種處理方式 寫死有越界的可能性

不知道有沒(méi)有其他的處理方式

問(wèn)題解答

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

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 榆树市| 永顺县| 禹州市| 勃利县| 和硕县| 延边| 南宁市| 封开县| 昌图县| 凭祥市| 湟源县| 隆安县| 板桥市| 海阳市| 合水县| 舟曲县| 科尔| 博乐市| 板桥市| 山阳县| 海晏县| 江安县| 定襄县| 台湾省| 云霄县| 长葛市| 晋江市| 孟连| 南漳县| 周宁县| 林西县| 酒泉市| 靖安县| 化隆| 建阳市| 贵州省| 丰镇市| 青神县| 阳江市| 武威市| 泉州市|