当前位置:网站首页>MytipartFile与File的相互转换
MytipartFile与File的相互转换
2022-06-28 10:51:00 【小玩偶啊】
1. MutipartFile 转换为File
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
2. File转换为MutipartFile
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
边栏推荐
猜你喜欢
随机推荐
Décompression par compression
appliedzkp zkevm(10)中的Transactions Proof
Katalon框架测试一个web页面操作实例代码
Mysql database overview and installation process
增量快照 必须要求mysql表有主键的吗?
Mysql通用二进制安装方式
[unity] built in rendering pipeline to URP
2022吴恩达机器学习specialization Week 2 practice lab: Linear regression
Guangzhou Customs supports the stable supply of food, agricultural products, traditional Chinese medicine and other civilian and biological resources to Hong Kong
Mongo数据库
MySQL (III)
知道 Redis RDB 这些细节,可以少踩很多坑
Teach you how to handle the reverse SVG mapping of JS
Discard Tkinter! Simple configuration to quickly generate cool GUI!
AQS理解
Please consult me. I run the MYSQL to MySQL full synchronization of flykcdc in my local ide. This is in my local ide
metersphere使用js刷新当前页面
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
Wireless communication module fixed-point transmission - point to multipoint specific transmission application
DlhSoft Kanban Library for WPF