Explorar el Código

南岸纠错提交

wrh hace 2 años
padre
commit
f0d46e9e59

+ 12 - 6
nngkxxdp/src/main/java/com/example/nngkxxdp/controller/RectificationController.java

@@ -35,12 +35,12 @@ public class RectificationController {
     }
 
     @PostMapping("/front/submit")
-    public Map<String,Object> submit(RectificationDO rectificationDO, MultipartFile file, HttpSession session) throws IOException {
+    public Map<String,Object> submit(RectificationDO rectificationDO, MultipartFile[] files, HttpSession session) throws IOException {
         String [] o={"captcha","errorUrl","equipment","mail","phone", "submitter", "questionDescription", "questionType", "uniqueCode"};
         if (!Blank.checkObjectParamNotNull(rectificationDO,o)){
             return SendUtil.send(false,ConstStr.REQUEST_WRONGPARAMS);
         }
-        if (Blank.isEmpty(file)) {
+        if (!Blank.isNotEmpty(files)) {
             return SendUtil.send(false,ConstStr.REQUEST_WRONGPARAMS);
         }
         if (!rectificationDO.getUniqueCode().equals(ConstStr.UNIQUE_CODE)) {
@@ -54,7 +54,7 @@ public class RectificationController {
         } else {
             return SendUtil.send(false,"验证码为空");
         }
-        return rectificationService.add(rectificationDO, file);
+        return rectificationService.add(rectificationDO, files);
     }
     
     @GetMapping("/list")
@@ -82,9 +82,15 @@ public class RectificationController {
     }
     
     @PostMapping("/add")
-    public Map<String,Object> add(@RequestBody RectificationDO rectificationDO) throws IOException {
-        // TODO 校验参数
-        return rectificationService.add(rectificationDO, null);
+    public Map<String,Object> add(RectificationDO rectificationDO, MultipartFile[] files) throws IOException {
+        String [] o={"captcha","errorUrl","equipment","mail","phone", "submitter", "questionDescription", "questionType"};
+        if (!Blank.checkObjectParamNotNull(rectificationDO,o)){
+            return SendUtil.send(false,ConstStr.REQUEST_WRONGPARAMS);
+        }
+        if (!Blank.isNotEmpty(files)) {
+            return SendUtil.send(false,ConstStr.REQUEST_WRONGPARAMS);
+        }
+        return rectificationService.add(rectificationDO, files);
     }
     
     @PostMapping("/update")

+ 2 - 2
nngkxxdp/src/main/java/com/example/nngkxxdp/service/RectificationService.java

@@ -57,10 +57,10 @@ public interface RectificationService {
      * @author zwq
      * @date 2023/01/30 10:44
      * @param rectificationDO 实例对象
-     * @param file 文件对象
+     * @param files 文件对象
      * @return java.util.Map<java.lang.String,java.lang.Object> 数据
      */
-    Map<String, Object> add(RectificationDO rectificationDO, MultipartFile file) throws IOException;
+    Map<String, Object> add(RectificationDO rectificationDO, MultipartFile[] files) throws IOException;
 
     /**
      * description: 修改

+ 22 - 28
nngkxxdp/src/main/java/com/example/nngkxxdp/service/impl/RectificationServiceImpl.java

@@ -2,9 +2,7 @@ package com.example.nngkxxdp.service.impl;
 
 import cn.hutool.core.io.FileTypeUtil;
 import cn.hutool.core.lang.UUID;
-import com.example.nngkxxdp.dao.FileDao;
 import com.example.nngkxxdp.dao.RectificationDao;
-import com.example.nngkxxdp.entity.SFile;
 import com.example.nngkxxdp.service.RectificationService;
 import com.example.nngkxxdp.util.Blank;
 import com.example.nngkxxdp.util.ConstStr;
@@ -19,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -39,9 +38,6 @@ public class RectificationServiceImpl implements RectificationService {
     @Value("${file.location}")
     private String location;
 
-    @Resource
-    private FileDao fileDao;
-
     /**
      * description: 详情
      *
@@ -103,47 +99,45 @@ public class RectificationServiceImpl implements RectificationService {
      * description: 新增
      *
      * @param rectificationDO 实例对象
-     * @param file            文件对象
+     * @param files            文件对象
      * @return java.util.Map<java.lang.String, java.lang.Object> 数据
      * @author zwq
      * @date 2023/01/30 10:44
      */
     @Override
-    public Map<String, Object> add(RectificationDO rectificationDO, MultipartFile file) throws IOException {
+    public Map<String, Object> add(RectificationDO rectificationDO, MultipartFile[] files) throws IOException {
         if (!Blank.isPhone(rectificationDO.getPhone())) {
             return SendUtil.send(false, "请输入正确的手机号");
         }
         if (!Blank.isEmail(rectificationDO.getMail())) {
             return SendUtil.send(false, "请输入正确的邮箱");
         }
-        if (Blank.isNotEmpty(file)) {
+        if (Blank.isNotEmpty(files)) {
             File pathFile = new File(location);
             if (!pathFile.isDirectory()) {
                 if (!pathFile.mkdirs()) {
                     return SendUtil.send(false, ConstStr.ADD_FAILED, "");
                 }
             }
-            //获取上传过来的文件名
-            File temporaryFile = new File(location + file.getOriginalFilename());
-            file.transferTo(temporaryFile);
-            String type = FileTypeUtil.getType(temporaryFile);
-            if (!("jpg".equals(type) || "png".equals(type) || "bmp".equals(type) || "jpeg".equals(type))) {
-                return SendUtil.send(false, ConstStr.ADD_FAILED, "上传的格式必须是jpg或png或bmp或jpeg");
-            }
-            String newName = (new Date()).getTime() + ((int) (Math.random() * 9000) + 1000) + "." + type;
-            String filePath = location + newName;
-            File newFile = new File(filePath);
-            if (!temporaryFile.renameTo(newFile)) {
-                return SendUtil.send(false, ConstStr.ADD_FAILED, "");
-            }
-            // 增加数据到s_file表
-            SFile sFile = new SFile();
-            sFile.setPath(newName);
-            sFile.setSuffix(type);
-            if (fileDao.addFile(sFile) <= 0) {
-                return SendUtil.send(false, ConstStr.ADD_FAILED, "");
+            ArrayList<String> picPathList = new ArrayList<>();
+            for (MultipartFile file : files) {
+                //获取上传过来的文件名
+                File temporaryFile = new File(location + file.getOriginalFilename());
+                file.transferTo(temporaryFile);
+                String type = FileTypeUtil.getType(temporaryFile);
+                if (!("jpg".equals(type) || "png".equals(type) || "bmp".equals(type) || "jpeg".equals(type))) {
+                    return SendUtil.send(false, ConstStr.ADD_FAILED, "上传的格式必须是jpg或png或bmp或jpeg");
+                }
+                String newName = (new Date()).getTime() + ((int) (Math.random() * 9000) + 1000) + "." + type;
+                String filePath = location + newName;
+                File newFile = new File(filePath);
+                if (!temporaryFile.renameTo(newFile)) {
+                    return SendUtil.send(false, ConstStr.ADD_FAILED, "");
+                }
+                picPathList.add(newName);
             }
-            rectificationDO.setPicture(newName);
+            String picPath = String.join(",", picPathList);
+            rectificationDO.setPicture(picPath);
         }
         rectificationDO.setId(UUID.randomUUID().toString());
         rectificationDO.setCreateTime(new Date());