java - inputstream轉(zhuǎn)為byte數(shù)組 數(shù)組越界
問題描述
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;
相關(guān)文章:
1. python執(zhí)行cmd命令,怎么讓他執(zhí)行類似Ctrl+C效果將其結(jié)束命令?2. python - Flask寫的注冊頁面,當(dāng)注冊時,如果填寫數(shù)據(jù)庫里有的相同數(shù)據(jù),就報錯3. python - Django有哪些成功項目?4. python - scrapy url去重5. 實現(xiàn)bing搜索工具urlAPI提交6. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)7. mysql在限制條件下篩選某列數(shù)據(jù)相同的值8. 關(guān)于mysql聯(lián)合查詢一對多的顯示結(jié)果問題9. 數(shù)據(jù)庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。10. Python從URL中提取域名
