|
@@ -1,16 +1,20 @@
|
|
|
-package com.ruoyi.system.service.impl;
|
|
|
+package com.ruoyi.framework.recovery.service.impl;
|
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.utils.FTPUtil;
|
|
|
import com.ruoyi.common.utils.file.FileUploadUtils;
|
|
|
import com.ruoyi.common.utils.file.FileUtils;
|
|
|
import com.ruoyi.common.utils.uuid.UUID;
|
|
|
-import com.ruoyi.system.service.ICommonService;
|
|
|
+import com.ruoyi.framework.recovery.mapper.RecCommodityMapper;
|
|
|
+import com.ruoyi.framework.recovery.service.ICommonService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.InputStream;
|
|
@@ -25,18 +29,28 @@ public class CommonServiceImpl implements ICommonService {
|
|
|
@Autowired
|
|
|
private FTPUtil ftpUtil;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RecCommodityMapper commodityMapper;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 时间格式化
|
|
|
*/
|
|
|
- private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
@Override
|
|
|
- public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
|
|
-// 文件目录,以当天时间为目录
|
|
|
- String filePath = "recovery/" + simpleDateFormat.format(new Date()) + "/";
|
|
|
+ public AjaxResult uploadFile(MultipartFile file, String type) throws Exception {
|
|
|
+ // 高清文件目录,以当天时间为目录
|
|
|
+ String filePath = "recovery-hd/" + simpleDateFormat.format(new Date()) + "/";
|
|
|
+ // 缩略图文件目录
|
|
|
+ String fileThu = "";
|
|
|
String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
+ boolean isImg = false;
|
|
|
+ if (suffix.equalsIgnoreCase("jpg") || suffix.equalsIgnoreCase("jpeg")
|
|
|
+ || suffix.equalsIgnoreCase("png") || suffix.equalsIgnoreCase("gif")) {
|
|
|
+ isImg = true;
|
|
|
+ fileThu = "recovery-thum" + simpleDateFormat.format(new Date()) + "/";
|
|
|
+ }
|
|
|
// 生成新的文件名
|
|
|
String newFileName = UUID.randomUUID() + suffix;
|
|
|
File toFile = multipartFileToFile(file);
|
|
@@ -46,6 +60,15 @@ public class CommonServiceImpl implements ICommonService {
|
|
|
} else {
|
|
|
System.out.println("上传至ftp服务器失败!");
|
|
|
}
|
|
|
+ // 如果是图片,就上传缩略图
|
|
|
+ if (isImg) {
|
|
|
+ BufferedImage image = Thumbnails.of(file.getInputStream()).scale(0.8f).outputFormat(suffix).outputQuality(0.5).asBufferedImage();
|
|
|
+ File newFile = new File(fileThu + newFileName);
|
|
|
+ ImageIO.write(image, suffix.substring(1), newFile);
|
|
|
+ ftpUtil.uploadToFtp(fileThu, newFileName, newFile);
|
|
|
+ }
|
|
|
+ updateDb(filePath + newFileName, fileThu + newFileName, type);
|
|
|
+
|
|
|
// 上传并返回新文件名称
|
|
|
String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
@@ -87,4 +110,17 @@ public class CommonServiceImpl implements ICommonService {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新数据库内容
|
|
|
+ *
|
|
|
+ * @param hdUrl 高清图地址,若不是图片就文件地址
|
|
|
+ * @param thuUrl 缩略图地址
|
|
|
+ * @param type 更新到哪个表中
|
|
|
+ */
|
|
|
+ private void updateDb(String hdUrl, String thuUrl, String type) {
|
|
|
+ switch (type) {
|
|
|
+ case "commodity":
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|