perf: 可以上传多张图片,并且可以覆盖

dev
huangfeng 2 months ago
parent dcd2ecc1d8
commit fea13ffee7

@ -295,10 +295,10 @@ public class TerminalPhotoController extends BaseController {
@PostMapping("uploadPhoto")
@ApiOperation("上传图片")
@Log(title = "上传图片", type = "上传")
public ResponseReult<String> uploadPhoto(@RequestParam("file") MultipartFile file,
public ResponseReult<String> uploadPhoto(@RequestParam("files") MultipartFile[] files,
@RequestParam("cmdid") String cmdid) throws Exception {
if (file != null && file.getInputStream() != null) {
terminalPhotoService.upload(file, cmdid);
if (files != null && files.length > 0) {
terminalPhotoService.uploadPhotos(files, cmdid);
return ResponseReult.success("OK");
} else {
return ResponseReult.error("缺少上传文件");

@ -427,13 +427,21 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
}
@Override
public void upload(MultipartFile file, String cmdid) throws Exception {
public void uploadPhotos(MultipartFile[] files, String cmdid) throws Exception {
Terminals term = terminalExtService.getByCmdid(cmdid);
if (term == null) {
throw new ApiException(cmdid + "该装置不存在");
}
for (MultipartFile file : files) {
this.uploadOnePhoto(file, cmdid, term.getId());
}
}
private void uploadOnePhoto(MultipartFile file, String cmdid, Integer termId) throws Exception {
String suffix;
String hexC = "";
Long photoId;
Long time;
TerminalPhoto record = new TerminalPhoto();
try {
@ -441,7 +449,7 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
suffix = org.springframework.util.StringUtils.getFilenameExtension(fileName);
String prefix = fileName.replace("." + suffix, "");
String[] str = prefix.split("_");
Long photoId = Long.parseLong(str[1], 16);
photoId = Long.parseLong(str[1], 16);
Integer channel = Integer.parseInt(str[2], 16);
hexC = str[3];
Integer preset = Integer.parseInt(hexC, 16);
@ -467,7 +475,7 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
file.transferTo(dest);
log.info("成功上传一张图片" + filePath);
record.setTermId(term.getId());
record.setTermId(termId);
record.setMediaType(0);
record.setPath(filePath);
record.setRecvTime(BigInteger.valueOf(now.getTime() / 1000));
@ -488,7 +496,17 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
long fileSize = Files.size(Paths.get(C_PATH + filePath));
record.setFileSize((int) fileSize);
terminalPhotoDao.insert(record);
TerminalPhotoExample example = new TerminalPhotoExample();
TerminalPhotoExample.Criteria criteria = example.createCriteria();
criteria.andTermIdEqualTo(termId);
criteria.andOrginalIdEqualTo(photoId.intValue());
List<TerminalPhoto> list = terminalPhotoDao.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
terminalPhotoDao.insert(record);
} else {
record.setId(list.get(0).getId());
terminalPhotoDao.updateByPrimaryKey(record);
}
}
/**

@ -117,7 +117,7 @@ public interface TerminalPhotoService {
ServiceBody<String> takeAlarm(TerminalPhotoTestVo vo);
void upload(MultipartFile file, String cmdid) throws Exception;
void uploadPhotos(MultipartFile[] files, String cmdid) throws Exception;
}

Loading…
Cancel
Save