MultipartFile其实是一个接口,其中定义提供了上传文件的各方面信息
public interface MultipartFile extends InputStreamSource {
//获取文件的名字,这里指的是post表单里面定义的名字
String getName();
//获取文件的原名字,这里指的是本地文件真正的名字
@Nullable
String getOriginalFilename();
//文件的类型
@Nullable
String getContentType();
//文件是否为空
boolean isEmpty();
//文件的大小
long getSize();
//获取文件的byte数组
byte[] getBytes() throws IOException;
//以流的方式获取文件
InputStream getInputStream() throws IOException;
//将其转为Resource类型,可以将其视为文件资源
default Resource getResource() {
return new MultipartFileResource(this);
}
//将其转换为文件
void transferTo(File var1) throws IOException, IllegalStateException;
//通过提供文件路径的方式将其转换为文件
default void transferTo(Path dest) throws IOException, IllegalStateException {
FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));
}
}
存储图片
@PostMapping("/uploadImage")
@ResponseBody
public BaseGduiDTO<?> uploadImageFile(@RequestParam("img") MultipartFile uploadImage) throws Exception {
String fileName = uploadImage.getOriginalFilename();
String imgFilePath = "D:\\img";
File targetFile = new File(imgFilePath, fileName);
//保存
uploadImage.transferTo(targetFile);
return BaseGduiDTO.ok(imgFilePath+"\\"+fileName);
}