有时候上传文件时,我们会涉及到操作上传文件实体MultipartFile 做一些IO操作,但是有时无法操作文件,只能转换为file才可以。
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @Description:
* @date 2019/9/23 10:21
*/
public class MultipartFileToFileUtils {
/**
* MultipartFile 转 File
* @param file
* @throws Exception
*/
public static 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());
toFile = inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
private static File 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();
return file;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
/**
* 图片上传
*
* @param file 图片文件
* @return 图片地址
*/
@Override
public String uploadPicture(MultipartFile file) throws Exception {
//将MultipartFile 转换为File
File localFile = MultipartFileToFileUtils.multipartFileToFile(file);
String imgUrl = uploadUtils.uploadImg(localFile);
log.info("web upload image url:{}", imgUrl);
return imgUrl;
}