Kaynağa Gözat

上传图片优化

limingming 1 yıl önce
ebeveyn
işleme
02b74828b5

+ 10 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/platform/UploadController.java

@@ -2,6 +2,7 @@ package com.ruoyi.web.controller.platform;
 
 import com.ruoyi.common.config.RuoYiConfig;
 import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.utils.ConstantUtil;
 import com.ruoyi.common.utils.file.FileUploadUtils;
 import com.ruoyi.system.domain.UploadInfo;
 import org.springframework.web.bind.annotation.*;
@@ -22,6 +23,15 @@ public class UploadController {
     @PostMapping("/uploadImage")
     public R<UploadInfo> addSave(@RequestParam("file") MultipartFile file) throws IOException {
         //限制文件大小??
+        String originalImgFilename = file.getOriginalFilename();
+        String imgSuffix = originalImgFilename.substring(file.getOriginalFilename().lastIndexOf("."));
+        if (!ConstantUtil.PICTURE.contains(imgSuffix)) {
+            return R.fail("图片类型格式不支持");
+        }
+        //不超过5M
+        if (file.getSize() / 1024  > 5120 ){
+            return R.fail("图片大小超过限制");
+        }
         // 上传文件路径
         String filePath = RuoYiConfig.getUploadPath();
         // 上传并返回新文件名称

+ 15 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/ConstantUtil.java

@@ -0,0 +1,15 @@
+package com.ruoyi.common.utils;
+
+/**
+ * @Author CHAILEILEI
+ * @Time 2021/11/8 下午3:23
+ * @Version V1.0.0
+ */
+public class ConstantUtil {
+
+   public static final String PICTURE = ".png|.gif|.jpg|.jpeg";
+
+   public static final String APK = ".apk";
+
+   public static final String SDK = ".jar";
+}