Zhaops пре 6 година
родитељ
комит
eb33dbf9f3

+ 6 - 0
config-server/src/main/resources/shared/icssman-service-dev.yml

@@ -47,6 +47,12 @@ spring:
         login-username: root
         login-password: root
 
+  servlet:
+    multipart:
+      enabled: true
+      max-file-size: 1MB
+      max-request-size: 1MB
+
   cloud:
     stream:
       bindings:

+ 6 - 0
config-server/src/main/resources/shared/icssman-service-local.yml

@@ -47,6 +47,12 @@ spring:
         login-username: root
         login-password: root
 
+  servlet:
+    multipart:
+      enabled: true
+      max-file-size: 1MB
+      max-request-size: 1MB
+
   cloud:
     stream:
       bindings:

+ 6 - 0
config-server/src/main/resources/shared/icssman-service-pro.yml

@@ -47,6 +47,12 @@ spring:
         login-username: root
         login-password: root
 
+  servlet:
+    multipart:
+      enabled: true
+      max-file-size: 1MB
+      max-request-size: 1MB
+
   cloud:
     stream:
       bindings:

+ 6 - 0
config-server/src/main/resources/shared/icssman-service-test.yml

@@ -47,6 +47,12 @@ spring:
         login-username: root
         login-password: root
 
+  servlet:
+    multipart:
+      enabled: true
+      max-file-size: 1MB
+      max-request-size: 1MB
+
   cloud:
     stream:
       bindings:

+ 2 - 1
icssman-service/src/main/java/com/diagbot/exception/ServiceErrorCode.java

@@ -9,7 +9,8 @@ package com.diagbot.exception;
  */
 public enum ServiceErrorCode implements ErrorCode {
     FILE_UPLOAD_ERROE("12020001", "文件上传出错"),
-    FILE_DELETE_ERROR("12020002", "远程删除文件出错");
+    FILE_DELETE_ERROR("12020002", "远程删除文件出错"),
+    FILE_MAX_SIZE_LIMIT("12020003","文件大小超出限制");
 
     private String code;
     private String msg;

+ 12 - 8
icssman-service/src/main/java/com/diagbot/service/impl/UploadServiceImpl.java

@@ -26,12 +26,16 @@ public class UploadServiceImpl implements UploadService {
         if (file.isEmpty()) {
             throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件不能为空");
         }
+        //文件大小上限1M
+        if (file.getSize() > 1024 * 1024) {
+            throw new CommonException(ServiceErrorCode.FILE_MAX_SIZE_LIMIT, "文件上传失败,超出大小限制1MB");
+        }
         try {
             // Get the file and save it somewhere
             path = saveFile(file);
         } catch (Exception e) {
-            log.error("upload file failed", e);
-            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "upload file failed");
+            log.error("文件上传失败", e);
+            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
         }
         return path;
     }
@@ -57,12 +61,12 @@ public class UploadServiceImpl implements UploadService {
         try {
             fileAbsolutePath = FastDFSClient.upload(file);  //upload to fastdfs
         } catch (Exception e) {
-            log.error("upload file Exception!", e);
-            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "upload file Exception");
+            log.error("文件上传异常", e);
+            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传异常");
         }
         if (fileAbsolutePath == null) {
-            log.error("upload file failed,please upload again!");
-            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "upload file failed,please upload again");
+            log.error("文件上传失败,请重新上传");
+            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
         }
         String path = "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
         return path;
@@ -85,8 +89,8 @@ public class UploadServiceImpl implements UploadService {
             FastDFSClient.deleteFile(groupName, fileName);
         } catch (Exception e) {
             log.error("", e);
-            throw new CommonException(ServiceErrorCode.FILE_DELETE_ERROR, "delete file failed");
+            throw new CommonException(ServiceErrorCode.FILE_DELETE_ERROR, "文件删除失败");
         }
         return true;
     }
-}
+}