SpringMVC的时候我们引入了apache.commons.fileupload包来进行文件上传,在SpringBoot2中则不需要引入该jar包(当然,引入也是可以的)
这里先将上传单个文件的代码,做一份记录

@RestController
public class FileUploadController {

  private DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("/yyyy/MM/dd/", Locale.CHINA);

  @PostMapping("/upload")
  public String upload(MultipartFile filename, HttpServletRequest request) {
    var realPath = request.getServletContext().getRealPath("/");
    var format = dateFormat.format(LocalDate.now());
    var path = realPath + format;
    System.out.println(path);
    var file = new File(path);
    if (!file.exists()) {
      file.mkdirs();
    }
    var originalFilename = filename.getOriginalFilename();
    var newName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
    try {
      filename.transferTo(new File(file, newName));

      return request.getScheme()
          + "://"
          + request.getServerName()
          + ":"
          + request.getServerPort()+format+newName;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "";
  }
}

这里使用了时间来对文件进行文件夹划分,后期可以使用fastDFS或者minio来完成文件的划分,而不是在本地使用文件夹来划分

Q.E.D.