自学内容网 自学内容网

博客摘录「 SpringBoot大文件(百M以上)的上传下载实现技术」2024年8月2日

上传大附件问题如何设计?大附件上传时如果没有进度条,用户以为是系统的bug。需要将文件截断成文件分片,逐个上传分片;最后一个步骤是将文件分片合并成一个文件后上传的ftp服务器。上传分片接口如下所示。

@ResponseBody
@PostMapping(value = "/upload")
public Result<?> upload(@RequestParam("file") MultipartFile multipartFile, HteAttachInfoVo attachInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception {
   try {
      HteAttachInfoVo vo = null;
      String tempPath = com.haday.tp.attachment.core.util.FileUtil.concatPath(System.getProperty("java.io.tmpdir"), AttachConstants.TEMP_PATH, IdUtil.fastSimpleUUID(), multipartFile.getOriginalFilename());
      File tempFile = new File(tempPath);
      if (!tempFile.getParentFile().exists() && !tempFile.getParentFile().mkdirs()) {
         return Result.error("无法创建临时文件:" + tempPath);
      }
      try{
               multipartFile.transferTo(tempFile);
         vo = attachInfoService.uploadChunk(tempFile, attachInfoVo);
         response.setStatus(HttpServletResponse.SC_OK);
      } catch (Exception e) {
         e.printStackTrace();
         log.error("上传文件失败:{}", e.getMessage());
         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         return Result.error("上传文件失败:"+e.getMessage());
      } finally {
      }
      return Result.OK(vo);
   }catch (Exception e){
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return Result.error("上传文件失败:"+e.getMessage());
   }

}

下载时,将分片的byte按照序号先后写入输出流直到最后一个分片为止。


原文地址:https://blog.csdn.net/qq_39126115/article/details/140873819

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!