MultipartFile 转成InputStream
时间: 2023-10-12 22:03:10 浏览: 107
要将MultipartFile转换为InputStream,可以使用以下代码:
```java
public InputStream multipartToInputStream(MultipartFile multipartFile) throws IOException {
InputStream inputStream = null;
File file = null;
try {
file = File.createTempFile("temp", null);
multipartFile.transferTo(file);
inputStream = new FileInputStream(file);
} catch (Exception e) {
e.printStackTrace();
} finally {
file.deleteOnExit();
inputStream.close();
}
return inputStream;
}
```
这段代码首先创建一个临时文件,然后将MultipartFile对象转存到临时文件中。接着使用文件创建一个InputStream流对象,并返回该流对象作为结果。最后,记得在最终使用完InputStream后,删除临时文件并关闭流对象。这样就成功将MultipartFile转换为InputStream了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [MultipartFile转换为InputStream](https://blog.csdn.net/weixin_50726818/article/details/127052304)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [JAVA——InputStream与MultipartFile互转](https://blog.csdn.net/LCPMVP/article/details/126419581)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文