Browse Source

新增上传接口

Administrator 4 years ago
parent
commit
b9e584de79

+ 62 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/Base64DecodedMultipartFile.java

@@ -0,0 +1,62 @@
+package com.ruoyi.web.controller.common;
+
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.*;
+
+/**
+ * @author tjf
+ * @Date: 2021/05/31/15:57
+ */
+public class Base64DecodedMultipartFile implements MultipartFile {
+    private final byte[] imgContent;
+    private final String header;
+
+    public Base64DecodedMultipartFile(byte[] imgContent, String header) {
+        this.imgContent = imgContent;
+        this.header = header.split(";")[0];
+    }
+
+    @Override
+    public String getName() {
+        // TODO - implementation depends on your requirements
+        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
+    }
+
+    @Override
+    public String getOriginalFilename() {
+        // TODO - implementation depends on your requirements
+        return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
+    }
+
+    @Override
+    public String getContentType() {
+        // TODO - implementation depends on your requirements
+        return header.split(":")[1];
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return imgContent == null || imgContent.length == 0;
+    }
+
+    @Override
+    public long getSize() {
+        return imgContent.length;
+    }
+
+    @Override
+    public byte[] getBytes() {
+        return imgContent;
+    }
+
+    @Override
+    public InputStream getInputStream() {
+        return new ByteArrayInputStream(imgContent);
+    }
+
+    @Override
+    public void transferTo(File dest) throws IOException, IllegalStateException {
+        new FileOutputStream(dest).write(imgContent);
+    }
+}

+ 52 - 4
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java

@@ -6,10 +6,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import com.ruoyi.common.config.RuoYiConfig;
 import com.ruoyi.common.constant.Constants;
@@ -18,6 +15,9 @@ import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.FileUploadUtils;
 import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.framework.config.ServerConfig;
+import sun.misc.BASE64Decoder;
+
+import java.io.IOException;
 
 /**
  * 通用请求处理
@@ -89,6 +89,31 @@ public class CommonController
         }
     }
 
+    /**
+     * 通用上传请求
+     */
+    @PostMapping("/common/uploadBase")
+    public AjaxResult uploadFileBase64(@RequestBody String file) throws Exception
+    {
+        try
+        {
+            MultipartFile multipartFile = base64ToMultipart(file);
+            // 上传文件路径
+            String filePath = RuoYiConfig.getUploadPath();
+            // 上传并返回新文件名称
+            String fileName = FileUploadUtils.upload(filePath, multipartFile);
+            String url = serverConfig.getUrl() + fileName;
+            AjaxResult ajax = AjaxResult.success();
+            ajax.put("fileName", fileName);
+            ajax.put("url", url);
+            return ajax;
+        }
+        catch (Exception e)
+        {
+            return AjaxResult.error(e.getMessage());
+        }
+    }
+
     /**
      * 本地资源通用下载
      */
@@ -117,4 +142,27 @@ public class CommonController
             log.error("下载文件失败", e);
         }
     }
+
+    /**
+     * base64转MultipartFile
+     * @param base64
+     * @return
+     */
+    public static MultipartFile base64ToMultipart(String base64) {
+        try {
+            String[] baseStrs = base64.split(",");
+            BASE64Decoder decoder = new BASE64Decoder();
+            byte[] b;
+            b = decoder.decodeBuffer(baseStrs[1]);
+            for(int i = 0; i < b.length; ++i) {
+                if (b[i] < 0) {
+                    b[i] += 256;
+                }
+            }
+            return new Base64DecodedMultipartFile(b, baseStrs[0]);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
 }

+ 1 - 1
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MimeTypeUtils.java

@@ -28,7 +28,7 @@ public class MimeTypeUtils
             // 图片
             "bmp", "gif", "jpg", "jpeg", "png",
             // word excel powerpoint
-            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
+            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt","msword",
             // 压缩文件
             "rar", "zip", "gz", "bz2",
             // pdf

+ 1 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java

@@ -114,6 +114,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 .antMatchers("/*/api-docs").anonymous()
                 .antMatchers("/druid/**").anonymous()
                 .antMatchers("/common/upload").anonymous()
+                .antMatchers("/common/uploadBase").anonymous()
                 .antMatchers("/bmProjectReport/report").anonymous()
                 /*以下为测试新增,上线删除*/