perf: 改用异步线程队列保存上传的图片
parent
7986230a59
commit
1084a6c3ed
@ -0,0 +1,18 @@
|
||||
package com.shxy.xymanager_common.model;
|
||||
|
||||
import com.shxy.xymanager_common.entity.TerminalPhoto;
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Data
|
||||
public class PhotoPrepareModel {
|
||||
MultipartFile file;
|
||||
String cmdid;
|
||||
Integer termId;
|
||||
String hexC;
|
||||
TerminalPhoto record;
|
||||
Long photoId;
|
||||
Long time;
|
||||
String suffix;
|
||||
String fileName;
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
package com.shxy.xymanager_service.impl;
|
||||
|
||||
import com.shxy.xymanager_common.entity.TerminalPhoto;
|
||||
import com.shxy.xymanager_common.entity.TerminalPhotoExample;
|
||||
import com.shxy.xymanager_common.model.PhotoPrepareModel;
|
||||
import com.shxy.xymanager_common.util.DateUtil;
|
||||
import com.shxy.xymanager_dao.dao.TerminalPhotoDao;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static com.shxy.xymanager_service.impl.TerminalPhotoServiceImpl.C_PATH;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AsyncService {
|
||||
|
||||
@Resource
|
||||
TerminalPhotoDao terminalPhotoDao;
|
||||
|
||||
@Async
|
||||
public void savePreparePhotos(List<PhotoPrepareModel> list) {
|
||||
for (PhotoPrepareModel item : list) {
|
||||
this.saveOnePreparePhoto(item);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveOnePreparePhoto(PhotoPrepareModel item) {
|
||||
TerminalPhoto record = item.getRecord();
|
||||
String cmdid = item.getCmdid();
|
||||
MultipartFile file = item.getFile();
|
||||
String hexC = item.getHexC();
|
||||
Integer termId = item.getTermId();
|
||||
Long photoId = item.getPhotoId();
|
||||
Long time = item.getTime();
|
||||
String suffix = item.getSuffix();
|
||||
String fileName = item.getFileName();
|
||||
|
||||
int length = cmdid.length();
|
||||
Date now = new Date();
|
||||
String folder = DateUtil.format(now, "yyyy/MM/dd") + "/" + cmdid.substring(length - 2);
|
||||
File dir = new File(C_PATH + folder);
|
||||
dir.mkdirs();
|
||||
Date photoTime = new Date(time * 1000);
|
||||
String strTime = DateUtil.format(photoTime, "yyyyMMddHHmmss");
|
||||
String filename = cmdid + "_" + record.getChannelId() + "_" + hexC + "_" + strTime + "." + suffix;
|
||||
String filePath = folder + "/" + filename;
|
||||
|
||||
File dest = new File(C_PATH + filePath);
|
||||
try {
|
||||
file.transferTo(dest);
|
||||
log.info("成功上传一张图片" + filePath);
|
||||
} catch (IOException e) {
|
||||
log.error("保存上传的图片失败, cmdid=" + cmdid + ", fileName=" + fileName);
|
||||
return;
|
||||
}
|
||||
|
||||
record.setTermId(termId);
|
||||
record.setMediaType(0);
|
||||
record.setPath(filePath);
|
||||
record.setRecvTime(BigInteger.valueOf(now.getTime() / 1000));
|
||||
record.setRecvEndTime(BigInteger.valueOf(now.getTime() / 1000));
|
||||
record.setCreateTime(now);
|
||||
record.setFlags(0);
|
||||
record.setIsMark(0);
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(dest);
|
||||
if (image != null) {
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
record.setWidth(width);
|
||||
record.setHeight(height);
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
}
|
||||
try {
|
||||
long fileSize = Files.size(Paths.get(C_PATH + filePath));
|
||||
record.setFileSize((int) fileSize);
|
||||
} catch (IOException ignore) {
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue