Browse Source

fixed 文件上传服务相关接口优化

fushengqian 1 month ago
parent
commit
861908c1eb

+ 0 - 8
fuint-application/src/main/java/com/fuint/common/service/CouponGroupService.java

@@ -91,12 +91,4 @@ public interface CouponGroupService extends IService<MtCouponGroup> {
      * */
     String importSendCoupon(MultipartFile file, String operator, String filePath) throws BusinessCheckException;
 
-    /**
-     * 保存文件
-     *
-     * @param file excel文件
-     * @param request 操作者
-     * */
-     String saveExcelFile(MultipartFile file, HttpServletRequest request) throws Exception;
-
 }

+ 0 - 10
fuint-application/src/main/java/com/fuint/common/service/GoodsService.java

@@ -11,8 +11,6 @@ import com.fuint.repository.model.MtGoods;
 import com.fuint.repository.model.MtGoodsSku;
 import com.fuint.repository.model.MtGoodsSpec;
 import org.springframework.web.multipart.MultipartFile;
-
-import javax.servlet.http.HttpServletRequest;
 import java.lang.reflect.InvocationTargetException;
 import java.util.*;
 
@@ -154,14 +152,6 @@ public interface GoodsService {
      * */
     String getStoreIds(Integer goodsId);
 
-    /**
-     * 保存文件
-     *
-     * @param request
-     * @param file excel文件
-     * */
-    String saveGoodsFile(HttpServletRequest request, MultipartFile file) throws Exception;
-
     /**
      * 导入商品
      *

+ 23 - 0
fuint-application/src/main/java/com/fuint/common/service/UploadService.java

@@ -0,0 +1,23 @@
+package com.fuint.common.service;
+
+import org.springframework.web.multipart.MultipartFile;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 文件上传服务类
+ *
+ * Created by FSQ
+ * CopyRight https://www.fuint.cn
+ */
+public interface UploadService {
+
+    /**
+     * 保存文件
+     *
+     * @param request
+     * @param file excel文件
+     * @return
+     * */
+    String saveUploadFile(HttpServletRequest request, MultipartFile file) throws Exception;
+
+}

+ 0 - 32
fuint-application/src/main/java/com/fuint/common/service/impl/CouponGroupServiceImpl.java

@@ -494,36 +494,4 @@ public class CouponGroupServiceImpl extends ServiceImpl<MtCouponGroupMapper, MtC
         }
         return uuid;
     }
-
-    /**
-     * 保存文件
-     *
-     * @param file excel文件
-     * @param request
-     * */
-    public String saveExcelFile(MultipartFile file, HttpServletRequest request) throws Exception {
-        String fileName = file.getOriginalFilename();
-
-        String imageName = fileName.substring(fileName.lastIndexOf("."));
-        String pathRoot = env.getProperty("images.root");
-        if (pathRoot == null || StringUtil.isEmpty(pathRoot)) {
-            pathRoot = ResourceUtils.getURL("classpath:").getPath();
-        }
-        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-
-        String filePath = "/static/uploadFiles/"+ DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
-        String path = filePath + uuid + imageName;
-
-        try {
-            File tempFile = new File(pathRoot + path);
-            if (!tempFile.getParentFile().exists()) {
-                tempFile.getParentFile().mkdirs();
-            }
-            CommonUtil.saveMultipartFile(file, pathRoot + path);
-        } catch (Exception e) {
-            log.error("上传保存文件出错:", e.getMessage());
-        }
-
-        return path;
-    }
 }

+ 0 - 45
fuint-application/src/main/java/com/fuint/common/service/impl/GoodsServiceImpl.java

@@ -13,8 +13,6 @@ import com.fuint.common.enums.GoodsTypeEnum;
 import com.fuint.common.enums.StatusEnum;
 import com.fuint.common.enums.YesOrNoEnum;
 import com.fuint.common.service.*;
-import com.fuint.common.util.CommonUtil;
-import com.fuint.common.util.DateUtil;
 import com.fuint.common.util.XlsUtil;
 import com.fuint.framework.annoation.OperationServiceLog;
 import com.fuint.framework.exception.BusinessCheckException;
@@ -41,11 +39,7 @@ import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.ResourceUtils;
 import org.springframework.web.multipart.MultipartFile;
-
-import javax.servlet.http.HttpServletRequest;
-import java.io.File;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
@@ -91,11 +85,6 @@ public class GoodsServiceImpl extends ServiceImpl<MtGoodsMapper, MtGoods> implem
      * */
     private CouponService couponService;
 
-    /**
-     * 系统环境变量
-     * */
-    private Environment env;
-
     /**
      * 分页查询商品列表
      *
@@ -830,40 +819,6 @@ public class GoodsServiceImpl extends ServiceImpl<MtGoodsMapper, MtGoods> implem
         return storeIds.stream().collect(Collectors.joining(","));
     }
 
-    /**
-     * 保存文件
-     *
-     * @param file excel文件
-     * @param request
-     * */
-    public String saveGoodsFile(HttpServletRequest request, MultipartFile file) throws Exception {
-        if (file == null) {
-            throw new BusinessCheckException("上传文件出错!");
-        }
-        String fileName = file.getOriginalFilename();
-        String uploadPath = fileName.substring(fileName.lastIndexOf("."));
-        String pathRoot = env.getProperty("images.root");
-        if (pathRoot == null || StringUtil.isEmpty(pathRoot)) {
-            pathRoot = ResourceUtils.getURL("classpath:").getPath();
-        }
-        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-
-        String filePath = "/static/uploadFiles/"+ DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
-        String path = filePath + uuid + uploadPath;
-
-        try {
-            File tempFile = new File(pathRoot + path);
-            if (!tempFile.getParentFile().exists()) {
-                tempFile.getParentFile().mkdirs();
-            }
-            CommonUtil.saveMultipartFile(file, pathRoot + path);
-        } catch (Exception e) {
-            logger.error("上传商品保存文件出错:", e.getMessage());
-        }
-
-        return path;
-    }
-
     /**
      * 导入商品
      *

+ 73 - 0
fuint-application/src/main/java/com/fuint/common/service/impl/UploadServiceImpl.java

@@ -0,0 +1,73 @@
+package com.fuint.common.service.impl;
+
+import com.fuint.common.service.UploadService;
+import com.fuint.common.util.CommonUtil;
+import com.fuint.common.util.DateUtil;
+import com.fuint.framework.exception.BusinessCheckException;
+import com.fuint.utils.StringUtil;
+import lombok.AllArgsConstructor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Service;
+import org.springframework.util.ResourceUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.File;
+import java.util.Date;
+import java.util.UUID;
+
+/**
+ * 文件上传服务类
+ *
+ * Created by FSQ
+ * CopyRight https://www.fuint.cn
+ */
+@Service
+@AllArgsConstructor
+public class UploadServiceImpl implements UploadService {
+
+    private static final Logger logger = LoggerFactory.getLogger(UploadServiceImpl.class);
+
+    /**
+     * 环境变量
+     * */
+    private Environment env;
+
+    /**
+     * 保存文件
+     *
+     * @param file excel文件
+     * @param request
+     * @return
+     * */
+    public String saveUploadFile(HttpServletRequest request, MultipartFile file) throws Exception {
+        if (file == null) {
+            throw new BusinessCheckException("上传文件出错!");
+        }
+        String fileName = file.getOriginalFilename();
+        String uploadPath = fileName.substring(fileName.lastIndexOf("."));
+        String pathRoot = env.getProperty("images.root");
+        if (pathRoot == null || StringUtil.isEmpty(pathRoot)) {
+            pathRoot = ResourceUtils.getURL("classpath:").getPath();
+        }
+        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
+
+        String filePath = "/static/uploadFiles/"+ DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
+        String path = filePath + uuid + uploadPath;
+
+        try {
+            File tempFile = new File(pathRoot + path);
+            if (!tempFile.getParentFile().exists()) {
+                tempFile.getParentFile().mkdirs();
+            }
+            CommonUtil.saveMultipartFile(file, pathRoot + path);
+        } catch (Exception e) {
+            logger.error("上传文件保存出错:", e.getMessage());
+        }
+
+        return path;
+    }
+
+}

+ 7 - 1
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendCouponGroupController.java

@@ -4,6 +4,7 @@ import com.fuint.common.Constants;
 import com.fuint.common.dto.*;
 import com.fuint.common.enums.StatusEnum;
 import com.fuint.common.service.CouponGroupService;
+import com.fuint.common.service.UploadService;
 import com.fuint.common.util.TokenUtil;
 import com.fuint.common.util.XlsUtil;
 import com.fuint.framework.dto.ExcelExportDto;
@@ -61,6 +62,11 @@ public class BackendCouponGroupController extends BaseController {
      * */
     private ExportService exportService;
 
+    /**
+     * 上传文件服务接口
+     * */
+    private UploadService uploadService;
+
     /**
      * 查询卡券分组列表
      *
@@ -285,7 +291,7 @@ public class BackendCouponGroupController extends BaseController {
         String token = request.getHeader("Access-Token");
         AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
 
-        String filePath = couponGroupService.saveExcelFile(file, request);
+        String filePath = uploadService.saveUploadFile(request, file);
         String uuid = couponGroupService.importSendCoupon(file, accountInfo.getAccountName(), filePath);
         return getSuccessResult(uuid);
     }

+ 7 - 30
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendFileController.java

@@ -3,6 +3,7 @@ package com.fuint.module.backendApi.controller;
 import com.aliyun.oss.OSS;
 import com.fuint.common.dto.AccountInfo;
 import com.fuint.common.service.SettingService;
+import com.fuint.common.service.UploadService;
 import com.fuint.common.util.AliyunOssUtil;
 import com.fuint.common.util.CommonUtil;
 import com.fuint.common.util.DateUtil;
@@ -57,6 +58,11 @@ public class BackendFileController extends BaseController {
      * */
     private SettingService settingService;
 
+    /**
+     * 上传文件服务接口
+     * */
+    private UploadService uploadService;
+
     /**
      * 后台上传文件
      *
@@ -115,7 +121,7 @@ public class BackendFileController extends BaseController {
 
         // 保存文件
         try {
-            String fileName = saveFile(file);
+            String fileName = uploadService.saveUploadFile(request, file);
             String baseImage = settingService.getUploadBasePath();
             String filePath = baseImage + fileName;
             String url = filePath;
@@ -193,33 +199,4 @@ public class BackendFileController extends BaseController {
             logger.error("下载文件出错:account = {},message = {}", accountInfo.getAccountName(), ex.getMessage());
         }
     }
-
-    public String saveFile(MultipartFile file) throws Exception {
-        String fileName = file.getOriginalFilename();
-
-        String imageName = fileName.substring(fileName.lastIndexOf("."));
-
-        String pathRoot = env.getProperty("images.root");
-        if (pathRoot == null || StringUtil.isEmpty(pathRoot)) {
-            pathRoot = ResourceUtils.getURL("classpath:").getPath();
-        }
-        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-
-        String baseImage = env.getProperty("images.path");
-        String filePath = baseImage + DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
-
-        String path = filePath + uuid + imageName;
-
-        try {
-            File tempFile = new File(pathRoot + path);
-            if (!tempFile.getParentFile().exists()) {
-                tempFile.getParentFile().mkdirs();
-            }
-            CommonUtil.saveMultipartFile(file, pathRoot + path);
-        } catch (Exception e) {
-            throw new Exception("上传失败,请检查目录是否可写");
-        }
-
-        return path;
-    }
 }

+ 7 - 2
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendGoodsController.java

@@ -74,6 +74,11 @@ public class BackendGoodsController extends BaseController {
      * */
     private SettingService settingService;
 
+    /**
+     * 上传文件服务接口
+     * */
+    private UploadService uploadService;
+
     /**
      * 分页查询商品列表
      *
@@ -742,7 +747,7 @@ public class BackendGoodsController extends BaseController {
      * @param request
      * @throws
      */
-    @ApiOperation(value = "上传文件")
+    @ApiOperation(value = "上传商品导入文件")
     @RequestMapping(value = "/uploadGoodsFile", method = RequestMethod.POST)
     @CrossOrigin
     public ResponseObject uploadGoodsFile(HttpServletRequest request) throws Exception {
@@ -752,7 +757,7 @@ public class BackendGoodsController extends BaseController {
         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
         MultipartFile file = multipartRequest.getFile("file");
 
-        String filePath = goodsService.saveGoodsFile(request, file);
+        String filePath = uploadService.saveUploadFile(request, file);
         Boolean result = goodsService.importGoods(file, accountInfo, filePath);
 
         return getSuccessResult(result);