wuheng 2 lat temu
rodzic
commit
4d2ddcb49f

+ 2 - 2
pom.xml

@@ -6,7 +6,7 @@
 
     <groupId>com.lovecoding.uploadfiles</groupId>
     <artifactId>uploadfiles</artifactId>
-    <version>1.0-SNAPSHOT</version>
+    <version>2.0-SNAPSHOT</version>
 
     <parent>
         <artifactId>spring-boot-dependencies</artifactId>
@@ -14,7 +14,6 @@
         <version>2.3.3.RELEASE</version>
     </parent>
 
-
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -39,6 +38,7 @@
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>2.7.3</version>
                 <configuration>
                     <mainClass>com.lovecoding.upload.Application</mainClass>
                 </configuration>

+ 1 - 1
src/main/java/com/lovecoding/upload/config/CorsConfig.java

@@ -6,7 +6,7 @@ import org.springframework.web.filter.FormContentFilter;
 import org.springframework.web.servlet.config.annotation.CorsRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
-@Configuration
+//@Configuration
 public class CorsConfig implements WebMvcConfigurer {
 
     public void addCorsMappings(CorsRegistry registry) {

+ 9 - 5
src/main/java/com/lovecoding/upload/controller/FileController.java

@@ -1,6 +1,7 @@
 package com.lovecoding.upload.controller;
 
 import com.lovecoding.upload.config.FastDFSClientUtil;
+import com.lovecoding.upload.utils.CommonResult;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -9,6 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.util.HashMap;
+
 @RestController
 @RequestMapping("/file")
 public class FileController {
@@ -20,23 +23,24 @@ public class FileController {
     private String fileServerUrl ;
 
     @PostMapping("/upload")
-    public String uploadFile(MultipartFile file) {
+    public CommonResult uploadFile(MultipartFile file) {
+
         try {
             //判断文件是否存在
             if (file == null) {
-                throw new RuntimeException("文件不存在");
+                return CommonResult.failed("file is require");
             }
             //获取文件的完整名称
             String originalFilename = file.getOriginalFilename();
             if (StringUtils.isEmpty(originalFilename)) {
-                throw new RuntimeException("文件不存在");
+                return CommonResult.failed("file not find");
             }
             String url = fastDFSClientUtil.uploadFile(file);
-            return fileServerUrl+url;
+            return CommonResult.success(fileServerUrl+url);
 
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return "文件上传失败";
+        return CommonResult.failed("文件上传失败");
     }
 }

+ 111 - 0
src/main/java/com/lovecoding/upload/utils/CommonResult.java

@@ -0,0 +1,111 @@
+package com.lovecoding.upload.utils;
+
+public class CommonResult<T> {
+    private long code;
+    private String message;
+    private T data;
+
+    protected CommonResult() {
+    }
+
+    protected CommonResult(long code, String message, T data) {
+        this.code = code;
+        this.message = message;
+        this.data = data;
+    }
+
+    /**
+     * 成功返回结果
+     *
+     * @param data 获取的数据
+     */
+    public static <T> CommonResult<T> success(T data) {
+        return new CommonResult<T>(200, "操作成功", data);
+    }
+
+    /**
+     * 成功返回结果
+     *
+     * @param data 获取的数据
+     * @param  message 提示信息
+     */
+    public static <T> CommonResult<T> success(T data, String message) {
+        return new CommonResult<T>(200, message, data);
+    }
+
+    /**
+     * 失败返回结果
+     * @param message 错误信息
+     */
+    public static <T> CommonResult<T> failed(Long errorCode, String message) {
+        return new CommonResult<T>(errorCode, message, null);
+    }
+
+    /**
+     * 失败返回结果
+     * @param message 提示信息
+     */
+    public static <T> CommonResult<T> failed(String message) {
+        return new CommonResult<T>(500L, message, null);
+    }
+
+    /**
+     * 失败返回结果
+     */
+    public static <T> CommonResult<T> failed() {
+        return failed("操作失败");
+    }
+
+    /**
+     * 参数验证失败返回结果
+     */
+    public static <T> CommonResult<T> validateFailed() {
+        return failed("字段验证错误!");
+    }
+
+    /**
+     * 参数验证失败返回结果
+     * @param message 提示信息
+     */
+    public static <T> CommonResult<T> validateFailed(String message) {
+        return new CommonResult<T>(501L, message, null);
+    }
+
+    /**
+     * 未登录返回结果
+     */
+    public static <T> CommonResult<T> unauthorized(T data) {
+        return new CommonResult<T>(401, "请登陆后操作", data);
+    }
+
+    /**
+     * 未授权返回结果
+     */
+    public static <T> CommonResult<T> forbidden(T data) {
+        return new CommonResult<T>(402, "权限不足!", data);
+    }
+
+    public long getCode() {
+        return code;
+    }
+
+    public void setCode(long code) {
+        this.code = code;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public T getData() {
+        return data;
+    }
+
+    public void setData(T data) {
+        this.data = data;
+    }
+}

BIN
uploadfiles-2.0-SNAPSHOT.jar