1.文件上传请求
三个处理
a.上传文件到磁盘
文件路径:nebula:
file:
location: D:/文件备份/
+ temp/ UUIDUtil.getUUID() + originalAttachmentName的后缀
b.往表file_temp_attachment加记录(几个文件就加几个记录)
c.返回AttachmentDTO集合 (三个属性 file_temp_attachmen的前三个字段)
@PostMapping({"/temp/upload"})
public ResponseEntity<List<AttachmentDTO>> dealTempAttachmentUpload(@RequestParam("file") List<MultipartFile> multipartFiles) {
return new ResponseEntity(this.attachmentService.dealTempAttachmentUpload(multipartFiles, SecurityUtil.getOperatorId()), HttpStatus.OK);
}
返回多个AttachmentDTO
private String id; //临时文件表id
private String tempAttachmentName; //临时文件名称
private String originalAttachmentName; //临时文件原始名称
请求路径加前缀
@RestController
@RequestMapping(value = "/app/attachment")
public class AttachmentResource extends AttachmentCommonResource {
@Inject
public AttachmentResource(AttachmentService attachmentService) {
super(attachmentService);
}
2.用户、站点保存请求 createOfficialAttachment
两个处理:
a.把第一步的文件复制到指定位置 fileprefix + getSourceModule + 年 + 月 + 临时文件名称
b.根据上传时返回的AttachmentDTO往表file_attachment添加记录
private void saveLogo(AttachmentDTO logoDTO, String sourceId) {
List<AttachmentAddDTO> attachmentAddDTOList = new ArrayList<>();
AttachmentAddDTO attachmentAddDTO = new AttachmentAddDTO();
attachmentAddDTO.setSourceId(sourceId);
attachmentAddDTO.setTempAttachmentName(logoDTO.getTempAttachmentName());
attachmentAddDTO.setOriginalAttachmentName(logoDTO.getOriginalAttachmentName());
attachmentAddDTO.setSourceModule(FILE_MODULE_NAME);
attachmentAddDTOList.add(attachmentAddDTO);
attachmentService.createOfficialAttachment(attachmentAddDTOList, FILE_PREFIX, sourceId);
}
3.下载头像文件
根据id去查表file_attachment得到原始名称和路径
然后根据完成路径 fileprefix + getSourceModule + 年 + 月 + 临时文件名称去下载
public void dealDownloadAttachmentById(String id, HttpServletResponse response) {
Attachment attachment = (Attachment)this.attachmentRepository.selectByPrimaryKey(id);
if (attachment == null) {
log.error("附件:" + id + "不存在!");
throw new BasicRuntimeException("attachment", "10005");
} else {
this.downloadAttachment(attachment.getOriginalName(), attachment.getLocation(), attachment.getUrlAccess(), response);
}
}
4.createOfficialAttachment代码
@Transactional
public List<String> createOfficialAttachment(List<AttachmentAddDTO> attachmentAddDTOS, String prefix, String operatorId) {
Map<String, AttachmentAddDTO> attachmentAddDTOMap = new HashMap();
List<String> tempAttachmentNames = (List)attachmentAddDTOS.stream().map((attachmentDTO) -> {
attachmentAddDTOMap.put(attachmentDTO.getTempAttachmentName(), attachmentDTO);
return attachmentDTO.getTempAttachmentName();
}).collect(Collectors.toList());
List<String> noExistAttachments = new ArrayList();
List<String> existTempAttachments = this.validTempAttachmentIsExist(tempAttachmentNames, attachmentAddDTOMap, noExistAttachments);
if (!CollectionUtils.isEmpty(existTempAttachments)) {
LocalDateTime now = LocalDateTime.now();
List<Attachment> attachmentList = new ArrayList();
existTempAttachments.forEach((existTempAttachment) -> {
AttachmentAddDTO attachmentAddDTO = (AttachmentAddDTO)attachmentAddDTOMap.get(existTempAttachment);
Attachment attachment = new Attachment();
attachment.setId(UUIDUtil.getUUID());
attachment.setName(attachmentAddDTO.getTempAttachmentName());
attachment.setOriginalName(attachmentAddDTO.getOriginalAttachmentName());
attachment.setSourceModule(attachmentAddDTO.getSourceModule());
attachment.setSourceId(attachmentAddDTO.getSourceId());
attachment.setUrlAccess(attachmentAddDTO.isUrlAccess());
attachment.setMountFlag(attachmentAddDTO.isMountFlag());
attachment.setCreatorId(operatorId);
attachment.setCreateTime(now);
attachmentList.add(attachment);
String tempLocation = this.savePath + "temp" + File.separator + existTempAttachment;
StringBuilder location = new StringBuilder();
location.append(prefix).append(File.separator).append(attachmentAddDTO.getSourceModule()).append(File.separator).append(now.getYear()).append(File.separator).append(now.getMonth());
String basicDir = location.toString();
location.append(File.separator).append(existTempAttachment);
attachment.setLocation(location.toString());
String dir;
if (attachmentAddDTO.isUrlAccess()) {
dir = "access" + File.separator + basicDir;
} else {
dir = basicDir;
}
if (attachmentAddDTO.isMountFlag()) {
StringBuilder mountDir = new StringBuilder();
if (attachmentAddDTO.isMountAccess()) {
mountDir.append(this.savePath).append("mount").append(File.separator).append("access").append(File.separator).append(basicDir);
} else {
mountDir.append(this.savePath).append("mount").append(File.separator).append(basicDir);
}
this.createDir(mountDir.toString());
FileTransferUtil.copy(tempLocation, mountDir.append(File.separator).append(existTempAttachment).toString());
}
this.createDir(this.savePath + dir);
FileTransferUtil.move(tempLocation, this.savePath + dir + File.separator + existTempAttachment);
});
SFileTempAttachment sFileTempAttachment = SFileTempAttachment.fileTempAttachment;
((SQLDeleteClause)this.sqlQueryFactory.delete(sFileTempAttachment).where(sFileTempAttachment.ftaTempName.in(existTempAttachments))).execute();
this.attachmentRepository.insertList(attachmentList);
}
return noExistAttachments;
}