上传大附件问题如何设计?大附件上传时如果没有进度条,用户以为是系统的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按照序号先后写入输出流直到最后一个分片为止。













![[译] Go语言的源起,发展和未来](https://img-blog.csdnimg.cn/img_convert/c7231611d4b7b070948f773ef31c15c7.png)




