Ver código fonte

base64上传

shiqian 4 anos atrás
pai
commit
7a71f42e49

+ 21 - 0
boman-modules/boman-file/src/main/java/com/boman/file/controller/SysFileController.java

@@ -1,9 +1,12 @@
 package com.boman.file.controller;
 
+import com.boman.common.core.web.domain.AjaxResult;
+import com.boman.file.utils.FileUploadUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 import com.boman.common.core.domain.R;
@@ -45,4 +48,22 @@ public class SysFileController
             return R.fail(e.getMessage());
         }
     }
+
+    /**
+     * 通用上传请求
+     */
+    @PostMapping("/upload/base64")
+    public AjaxResult uploadFileBase64(@RequestBody String base64) {
+        try {
+            String url = sysFileService.uploadFileBase64(base64);
+            AjaxResult ajax = AjaxResult.success();
+            ajax.put("fileName", FileUtils.getName(url));
+            ajax.put("url", url);
+            return ajax;
+        } catch (Exception e) {
+            return AjaxResult.error(e.getMessage());
+        }
+    }
+
+
 }

+ 13 - 0
boman-modules/boman-file/src/main/java/com/boman/file/service/FastDfsSysFileServiceImpl.java

@@ -8,6 +8,8 @@ import org.springframework.web.multipart.MultipartFile;
 import com.github.tobato.fastdfs.domain.fdfs.StorePath;
 import com.github.tobato.fastdfs.service.FastFileStorageClient;
 
+import java.io.IOException;
+
 /**
  * FastDFS 文件存储
  * 
@@ -39,4 +41,15 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
                 FilenameUtils.getExtension(file.getOriginalFilename()), null);
         return domain + "/" + storePath.getFullPath();
     }
+
+    /**
+     * 功能描述: 上传base64
+     *
+     * @param base64 base64
+     * @return java.lang.String
+     */
+    @Override
+    public String uploadFileBase64(String base64) throws IOException {
+        return null;
+    }
 }

+ 10 - 0
boman-modules/boman-file/src/main/java/com/boman/file/service/ISysFileService.java

@@ -2,6 +2,8 @@ package com.boman.file.service;
 
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
+
 /**
  * 文件上传接口
  * 
@@ -17,4 +19,12 @@ public interface ISysFileService
      * @throws Exception
      */
     public String uploadFile(MultipartFile file) throws Exception;
+
+    /**
+     * 功能描述: 上传base64
+     *
+     * @param base64 base64
+     * @return java.lang.String
+     */
+    String uploadFileBase64(String base64) throws IOException;
 }

+ 15 - 0
boman-modules/boman-file/src/main/java/com/boman/file/service/LocalSysFileServiceImpl.java

@@ -7,6 +7,8 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 import com.boman.file.utils.FileUploadUtils;
 
+import java.io.IOException;
+
 /**
  * 本地文件存储
  * 
@@ -49,4 +51,17 @@ public class LocalSysFileServiceImpl implements ISysFileService
         String url = domain + localFilePrefix + name;
         return url;
     }
+
+    /**
+     * 功能描述: 上传base64
+     *
+     * @param base64 base64
+     * @return java.lang.String
+     */
+    @Override
+    public String uploadFileBase64(String base64) throws IOException {
+        MultipartFile multipartFile = FileUploadUtils.base64ToMultipart(base64);
+        String name = FileUploadUtils.upload(localFilePath, multipartFile);
+        return domain + localFilePrefix + name;
+    }
 }

+ 13 - 0
boman-modules/boman-file/src/main/java/com/boman/file/service/MinioSysFileServiceImpl.java

@@ -8,6 +8,8 @@ import com.boman.file.utils.FileUploadUtils;
 import io.minio.MinioClient;
 import io.minio.PutObjectArgs;
 
+import java.io.IOException;
+
 /**
  * Minio 文件存储
  * 
@@ -42,4 +44,15 @@ public class MinioSysFileServiceImpl implements ISysFileService
         client.putObject(args);
         return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
     }
+
+    /**
+     * 功能描述: 上传base64
+     *
+     * @param base64 base64
+     * @return java.lang.String
+     */
+    @Override
+    public String uploadFileBase64(String base64) throws IOException {
+        return null;
+    }
 }

+ 62 - 0
boman-modules/boman-file/src/main/java/com/boman/file/utils/Base64DecodedMultipartFile.java

@@ -0,0 +1,62 @@
+package com.boman.file.utils;
+
+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);
+    }
+}

+ 25 - 0
boman-modules/boman-file/src/main/java/com/boman/file/utils/FileUploadUtils.java

@@ -11,6 +11,7 @@ import com.boman.common.core.utils.DateUtils;
 import com.boman.common.core.utils.IdUtils;
 import com.boman.common.core.utils.StringUtils;
 import com.boman.common.core.utils.file.MimeTypeUtils;
+import sun.misc.BASE64Decoder;
 
 /**
  * 文件上传工具类
@@ -193,4 +194,28 @@ public class FileUploadUtils
         }
         return extension;
     }
+
+    /**
+     * 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;
+        }
+    }
+
 }