|
|
@@ -41,6 +41,7 @@ import java.net.Proxy;
|
|
|
import java.net.SocketTimeoutException;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
@@ -112,14 +113,23 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public R insertOrUpdateImg(MultipartFile file, String remark, Long categoryId, String id) {
|
|
|
+ public R insertOrUpdateImg(MultipartFile file, String remark, String createDate, Long categoryId, String id) {
|
|
|
String imageUrl = "";
|
|
|
String imageSize = "";
|
|
|
String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
|
|
|
String ftpThumbnailBasePath = InitRunner.dicCodeMap.get("ftp_thumbnail_basepath").getCodeValue();
|
|
|
String ftpBaseUrl = InitRunner.dicCodeMap.get("ftp_baseurl").getCodeValue();
|
|
|
- String parentPath = LocalDate.now().format(DateUtils.dateFormatter5);
|
|
|
+ List<String> delPathList = new ArrayList<>();
|
|
|
if (StringUtils.isEmpty(id)) {
|
|
|
+ String parentPath;
|
|
|
+ LocalDateTime createTime = LocalDateTime.now();
|
|
|
+ if (StringUtils.isNotEmpty(createDate)) {
|
|
|
+ createTime = LocalDateTime.parse(createDate, DateUtils.dateTimeFormatter);
|
|
|
+ parentPath = createTime.format(DateUtils.dateFormatter6);
|
|
|
+ } else {
|
|
|
+ parentPath = LocalDate.now().format(DateUtils.dateFormatter6);
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
// 1、给上传的图片生成新的文件名
|
|
|
// 1.1获取原始文件名
|
|
|
@@ -133,13 +143,16 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
|
|
|
InputStream input = file.getInputStream();
|
|
|
// 2.2调用FtpUtil工具类进行上传
|
|
|
boolean result = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
|
|
|
+ delPathList.add(ftpBasePath + parentPath + "/" + newName);
|
|
|
// 2.3缩略图
|
|
|
BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
|
|
- BufferedImage thumbnailImage = Thumbnails.of(originalImage).size(300, 200).asBufferedImage();
|
|
|
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(thumbnailImage, "jpg", thumbnailOutputStream);
|
|
|
+ Thumbnails.of(originalImage).size(300, 200).outputFormat(oldName.substring(oldName.lastIndexOf(".") + 1)).toOutputStream(thumbnailOutputStream);
|
|
|
ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
|
|
|
FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
|
|
|
+ delPathList.add(ftpThumbnailBasePath + parentPath + "/" + newName);
|
|
|
+ thumbnailOutputStream.close();
|
|
|
+ thumbnailInputStream.close();
|
|
|
if (result) {
|
|
|
//返回给前端图片访问路径
|
|
|
imageUrl = parentPath + "/" + newName;
|
|
|
@@ -152,10 +165,17 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
|
|
|
fileImage.setPath(imageUrl);
|
|
|
fileImage.setRemark(remark);
|
|
|
fileImage.setCategoryId(categoryId);
|
|
|
+ fileImage.setCreateTime(createTime);
|
|
|
pictureInfoMapper.insertFileImage(fileImage);
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 异常,删除已经上传的文件
|
|
|
+ if (!delPathList.isEmpty()) {
|
|
|
+ delPathList.forEach(FtpUtil::delFile);
|
|
|
+ }
|
|
|
+
|
|
|
log.error("insertOrUpdateImg Exception,", e);
|
|
|
+ throw new BusinessException(30000, e.getMessage());
|
|
|
}
|
|
|
|
|
|
JSONObject result = new JSONObject();
|
|
|
@@ -174,46 +194,71 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
|
|
|
fileImage.setRemark(remark);
|
|
|
}
|
|
|
|
|
|
+ String parentPath;
|
|
|
+ if (StringUtils.isNotEmpty(createDate)) {
|
|
|
+ LocalDateTime createTime = LocalDateTime.parse(createDate, DateUtils.dateTimeFormatter);
|
|
|
+ parentPath = createTime.format(DateUtils.dateFormatter6);
|
|
|
+ fileImage.setCreateTime(createTime);
|
|
|
+ } else {
|
|
|
+ parentPath = fileImage.getCreateTime().format(DateUtils.dateFormatter6);
|
|
|
+ }
|
|
|
+
|
|
|
if (file != null && file.getSize() > 0) {
|
|
|
try {
|
|
|
- boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
|
|
|
- FtpUtil.delFile(ftpThumbnailBasePath + fileImage.getPath());
|
|
|
- if (flag) {
|
|
|
- String oldName = file.getOriginalFilename();
|
|
|
- String newName = FtpUtil.genImageName();
|
|
|
- newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
- InputStream input = file.getInputStream();
|
|
|
- // 2.2调用FtpUtil工具类进行上传
|
|
|
- boolean result = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
|
|
|
- // 2.3缩略图
|
|
|
- BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
|
|
- BufferedImage thumbnailImage = Thumbnails.of(originalImage).size(300, 200).asBufferedImage();
|
|
|
- ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(thumbnailImage, "jpg", thumbnailOutputStream);
|
|
|
- ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
|
|
|
- FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
|
|
|
- if (result) {
|
|
|
- imageUrl = parentPath + "/" + newName;
|
|
|
- imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
|
|
|
-
|
|
|
- fileImage.setOldName(oldName);
|
|
|
- fileImage.setNewName(newName);
|
|
|
- fileImage.setSize(imageSize);
|
|
|
- fileImage.setPath(imageUrl);
|
|
|
- } else {
|
|
|
- throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
|
|
|
- }
|
|
|
+ // 1、给上传的图片生成新的文件名
|
|
|
+ // 1.1获取原始文件名
|
|
|
+ String oldName = file.getOriginalFilename();
|
|
|
+ String newName = FtpUtil.genImageName();
|
|
|
+ // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
|
|
|
+ newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
+ // 2、把图片上传到图片服务器
|
|
|
+ // 2.1获取上传的io流
|
|
|
+ InputStream input = file.getInputStream();
|
|
|
+ // 2.2调用FtpUtil工具类进行上传
|
|
|
+ boolean result1 = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
|
|
|
+ delPathList.add(ftpBasePath + parentPath + "/" + newName);
|
|
|
+ // 2.3缩略图
|
|
|
+ BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
|
|
+ ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
|
|
|
+ Thumbnails.of(originalImage).size(300, 200).outputFormat(oldName.substring(oldName.lastIndexOf(".") + 1)).toOutputStream(thumbnailOutputStream);
|
|
|
+ ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
|
|
|
+ boolean result2 = FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
|
|
|
+ delPathList.add(ftpThumbnailBasePath + parentPath + "/" + newName);
|
|
|
+ thumbnailOutputStream.close();
|
|
|
+ thumbnailInputStream.close();
|
|
|
+
|
|
|
+ if (result2 && result2) {
|
|
|
+ String oriPath = fileImage.getPath();
|
|
|
+
|
|
|
+ imageUrl = parentPath + "/" + newName;
|
|
|
+ imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
|
|
|
|
|
|
+ fileImage.setOldName(oldName);
|
|
|
+ fileImage.setNewName(newName);
|
|
|
+ fileImage.setSize(imageSize);
|
|
|
+ fileImage.setPath(imageUrl);
|
|
|
+ pictureInfoMapper.updateFileImage(fileImage);
|
|
|
+
|
|
|
+ FtpUtil.delFile(ftpBasePath + oriPath);
|
|
|
+ FtpUtil.delFile(ftpThumbnailBasePath + oriPath);
|
|
|
} else {
|
|
|
- throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "删除旧文件失败!");
|
|
|
+ delPathList.forEach(FtpUtil::delFile);
|
|
|
+
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return R.error().message(e.getMessage());
|
|
|
+ // 异常,删除已经上传的文件
|
|
|
+ if (!delPathList.isEmpty()) {
|
|
|
+ delPathList.forEach(FtpUtil::delFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.error("insertOrUpdateImg Exception,", e);
|
|
|
+ throw new BusinessException(30000, e.getMessage());
|
|
|
}
|
|
|
+ } else {
|
|
|
+ pictureInfoMapper.updateFileImage(fileImage);
|
|
|
}
|
|
|
|
|
|
- pictureInfoMapper.updateFileImage(fileImage);
|
|
|
return R.ok().data("success");
|
|
|
}
|
|
|
}
|