|
|
|
@ -4,6 +4,8 @@ import cn.hutool.core.img.ImgUtil;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import com.docus.core.util.Func;
|
|
|
|
|
import com.docus.core.util.ParamsUtils;
|
|
|
|
|
import com.docus.core.util.StopWatch;
|
|
|
|
|
import com.docus.infrastructure.web.exception.ApiException;
|
|
|
|
|
import com.docus.infrastructure.web.exception.ExceptionCode;
|
|
|
|
|
import com.docus.server.common.service.IFileUploadService;
|
|
|
|
@ -43,6 +45,9 @@ public class FileUploadServiceImpl implements IFileUploadService {
|
|
|
|
|
@Value("${file.uploadFolder:D://docus/source/}")
|
|
|
|
|
private String saveFilePath;
|
|
|
|
|
|
|
|
|
|
@Value("${file.uploadCompressFolder:#{null}}")
|
|
|
|
|
private String saveCompressFilePath;
|
|
|
|
|
|
|
|
|
|
@Value("${file.uploadCutFolder:#{null}}")
|
|
|
|
|
private String saveCutFilePath;
|
|
|
|
|
|
|
|
|
@ -63,13 +68,36 @@ public class FileUploadServiceImpl implements IFileUploadService {
|
|
|
|
|
|
|
|
|
|
valid(multipartFile);
|
|
|
|
|
|
|
|
|
|
uploadFileVOList.add(actionUploadFile(multipartFile, pathKey));
|
|
|
|
|
uploadFileVOList.add(actionUploadFile(multipartFile, pathKey, null, null, null));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return uploadFileVOList;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UploadFileVO actionUploadFile(MultipartFile multipartFile, String pathKey) throws Exception {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<UploadFileVO> uploadFile(MultipartFile[] multipartFiles, String pathKey, Double height, Double widthStart, Double widthEnd) throws Exception {
|
|
|
|
|
if (Func.isBlank(pathKey)) {
|
|
|
|
|
pathKey = UPLOAD_FOLDER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<UploadFileVO> uploadFileVOList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (MultipartFile multipartFile : multipartFiles) {
|
|
|
|
|
|
|
|
|
|
valid(multipartFile);
|
|
|
|
|
|
|
|
|
|
StopWatch watch = new StopWatch();
|
|
|
|
|
|
|
|
|
|
uploadFileVOList.add(actionUploadFile(multipartFile, pathKey, height, widthStart, widthEnd));
|
|
|
|
|
|
|
|
|
|
System.out.println(multipartFile.getOriginalFilename() + "====图片压缩和剪切总耗时=======>" + watch.elapsedTime() + "(ms)" + "<===========");
|
|
|
|
|
}
|
|
|
|
|
return uploadFileVOList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UploadFileVO actionUploadFile(MultipartFile multipartFile, String pathKey, Double compressWidth, Double widthStart, Double heightStart) throws Exception {
|
|
|
|
|
|
|
|
|
|
String todayFormat = ymdDtf.format(LocalDateTime.now());
|
|
|
|
|
|
|
|
|
@ -91,6 +119,23 @@ public class FileUploadServiceImpl implements IFileUploadService {
|
|
|
|
|
uploadFileVO.setFileType(multipartFile.getContentType());
|
|
|
|
|
uploadFileVO.setFilePath(fileName);
|
|
|
|
|
uploadFileVO.setSaveFilePath(saveFilePath);
|
|
|
|
|
uploadFileVO.setParams(ParamsUtils.addParam("compressWidth", compressWidth).addParam("widthStart", widthStart).addParam("heightStart", heightStart).param());
|
|
|
|
|
|
|
|
|
|
if (Func.isNotBlank(saveCompressFilePath)) {
|
|
|
|
|
File compressFileDir = new File(saveCompressFilePath + path);
|
|
|
|
|
if (!compressFileDir.exists()) {
|
|
|
|
|
compressFileDir.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
File compressDest = new File(saveCompressFilePath + fileName);
|
|
|
|
|
|
|
|
|
|
BufferedImage image = ImageIO.read(multipartFile.getInputStream());
|
|
|
|
|
int width = image.getWidth();
|
|
|
|
|
|
|
|
|
|
//图片压缩
|
|
|
|
|
ImgUtil.scale(image, FileUtil.file(compressDest), (float) (compressWidth / width));
|
|
|
|
|
|
|
|
|
|
uploadFileVO.setSaveCompressFilePath(saveCompressFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Func.isNotBlank(saveCutFilePath)) {
|
|
|
|
|
File cutFileDir = new File(saveCutFilePath + path);
|
|
|
|
@ -99,13 +144,14 @@ public class FileUploadServiceImpl implements IFileUploadService {
|
|
|
|
|
}
|
|
|
|
|
File cutDest = new File(saveCutFilePath + fileName);
|
|
|
|
|
|
|
|
|
|
BufferedImage image = ImageIO.read(multipartFile.getInputStream());
|
|
|
|
|
BufferedImage image = ImageIO.read(FileUtil.file(saveCompressFilePath + fileName));
|
|
|
|
|
int height = image.getHeight();
|
|
|
|
|
int width = image.getWidth();
|
|
|
|
|
int minX = image.getMinX();
|
|
|
|
|
int minY = image.getMinY();
|
|
|
|
|
|
|
|
|
|
ImgUtil.cut(image, FileUtil.file(cutDest), new Rectangle(50, 50, width, (height / 2) / 2));
|
|
|
|
|
//剪切
|
|
|
|
|
ImgUtil.cut(image, FileUtil.file(cutDest), new Rectangle(0, 0, widthStart.intValue(), heightStart.intValue()));
|
|
|
|
|
|
|
|
|
|
uploadFileVO.setWidth(width);
|
|
|
|
|
uploadFileVO.setHeight(height);
|
|
|
|
@ -199,10 +245,10 @@ public class FileUploadServiceImpl implements IFileUploadService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据图片的存储类型读取图片
|
|
|
|
|
*
|
|
|
|
|
* @param fileStorageType 文件存放类型 3 共享文件夹 1本地 2Ftp
|
|
|
|
|
* @param fileSrc 文件地址
|
|
|
|
|
* @return 返回图片对象
|
|
|
|
|
*/
|
|
|
|
|