MultipartFile 转成FileInputStream
时间: 2023-10-17 11:34:24 浏览: 183
要将MultipartFile转换为FileInputStream,您可以使用以下代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
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转换为临时文件,并使用FileInputStream从文件中创建InputStream。请注意,在转换过程中,需要确保使用`transferTo(file)`方法将MultipartFile写入临时文件。最后,记得在适当的地方关闭InputStream并删除临时文件以释放资源。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [简单实现Android文件上传](https://download.csdn.net/download/weixin_38631599/14025263)[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 ]
阅读全文