base on Springboot

config

  1. # 默认的文件大小限制
  2. spring.servlet.multipart.max-file-size=1MB
  3. # 默认的请求大小限制
  4. spring.servlet.multipart.max-request-size=10MB
  5. # 这是自定义的参数,用于指定上载路径
  6. logic.system.file.upload.path.local=e://opt/transocean/upload/
  7. logic.system.file.upload.path.remote=192.168.1.34/uploads/

rest&&service

原理:
会产生一个临时文件用于保存前的一些自定义操作
MultipartFiletransferTo方法将临时文件保存到指定目录

  • 默认的行为是移动临时文件到目标目录
  • 如果不行,将调用IOUtil的copy方法复制一份

tips:

  • 目标目录不能是一个还未创建的目录
    1. @PostMapping("/upload")
    2. public Response uploadData(@RequestParam("files") MultipartFile[] files) throws IOException {
    3. return ReturnUtil.returnSuccess(systemFileService.uploadData(Arrays.asList(files)));
    4. }
    ```java @Value(“${logic.system.file.upload.path.local}”) private String localPath;

@Value(“${logic.system.file.upload.path.remote}”) private String remotePath;

@Override public void uploadData(List files) throws IOException { if (StrUtil.isBlank(localPath)) { throw new BusinessException(“the upload path haven’t been set, please contact your system administrator”); } for (MultipartFile file : files) { String filename = file.getOriginalFilename(); String pathFileName = UUID.fastUUID() + “-“ + filename; String localPath = this.localPath + pathFileName; String remotePath = this.remotePath + pathFileName;

  1. File localFile = new File(localPath);
  2. file.transferTo(localFile);
  3. }

} ```