outputstream转byte数组、
时间: 2023-10-23 21:13:11 浏览: 281
浅谈java的byte数组的不同写法
inputstream转byte数组、byte数组转inputstream、multipartfile转byte数组、byte数组转multipartfile以及string转byte数组的方法。
将outputstream转为byte数组的方法是将outputstream对象转为bytearrayoutputstream对象,然后调用toByteArray()方法即可获得byte数组。例如:
```
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 向outputStream写入数据
byte[] byteArray = outputStream.toByteArray();
```
将inputstream转为byte数组的方法是创建一个ByteArrayOutputStream对象,然后使用BufferedInputStream从inputstream中读取数据,并通过write方法写入bytearrayoutputstream对象中,最后调用toByteArray()方法即可获得byte数组。例如:
```
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [javaint转byte数组](https://blog.csdn.net/weixin_39631301/article/details/114193828)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [byte数组、Blob、inputStream、outputStream、MultipartFile之间的转换](https://blog.csdn.net/zyxwvuuvwxyz/article/details/103163956)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文