gushoubang 2 miesięcy temu
rodzic
commit
cd0e992034

+ 9 - 22
siwei-modules/siwei-file/src/main/java/com/siwei/file/controller/SysFileController.java

@@ -4,6 +4,7 @@ import com.siwei.file.service.ISysFileService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
@@ -24,37 +25,23 @@ public class SysFileController {
     private ISysFileService sysFileService;
 
     /**
-     * 文件上传请求
+     * 域名或本机访问地址
      */
-    @PostMapping("upload")
-    public R<SysFile> upload(MultipartFile file) {
-        try {
-            // 上传并返回访问地址
-            String url = sysFileService.uploadFile(file);
-            SysFile sysFile = new SysFile();
-            sysFile.setName(FileUtils.getName(url));
-            sysFile.setUrl(url);
-            return R.ok(sysFile);
-        } catch (Exception e) {
-            log.error("上传文件失败", e);
-            return R.fail(e.getMessage());
-        }
-    }
+    @Value("${file.domain}")
+    public String domain;
 
     /**
      * 文件上传请求
      */
-    @PostMapping("upload/v1")
-    public R<SysFile> uploadV1(MultipartFile file) {
+    @PostMapping("upload")
+    public R<SysFile> upload(MultipartFile file) {
         try {
             // 上传并返回访问地址
-            String[] data = sysFileService.uploadFileV1(file);
-            String url = data[0];
-            String path = data[1];
+            String path = sysFileService.uploadFile(file);
             SysFile sysFile = new SysFile();
-            sysFile.setName(FileUtils.getName(url));
-            sysFile.setUrl(url);
+            sysFile.setName(FileUtils.getName(path));
             sysFile.setPath(path);
+            sysFile.setUrl(domain);
             return R.ok(sysFile);
         } catch (Exception e) {
             log.error("上传文件失败", e);

+ 6 - 16
siwei-modules/siwei-file/src/main/java/com/siwei/file/service/FastDfsSysFileServiceImpl.java

@@ -1,6 +1,7 @@
 package com.siwei.file.service;
 
 import java.io.InputStream;
+
 import com.alibaba.nacos.common.utils.IoUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -16,8 +17,7 @@ import com.siwei.common.core.utils.file.FileTypeUtils;
  * @author siwei-zhx
  */
 @Service
-public class FastDfsSysFileServiceImpl implements ISysFileService
-{
+public class FastDfsSysFileServiceImpl implements ISysFileService {
     /**
      * 域名或本机访问地址
      */
@@ -35,27 +35,17 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
      * @throws Exception
      */
     @Override
-    public String uploadFile(MultipartFile file) throws Exception
-    {
+    public String uploadFile(MultipartFile file) throws Exception {
         InputStream inputStream = null;
-        try
-        {
+        try {
             inputStream = file.getInputStream();
             StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FileTypeUtils.getExtension(file), null);
             return domain + "/" + storePath.getFullPath();
-        }
-        catch (Exception e)
-        {
+        } catch (Exception e) {
             throw new RuntimeException("FastDfs Failed to upload file", e);
-        }
-        finally
-        {
+        } finally {
             IoUtils.closeQuietly(inputStream);
         }
     }
 
-    @Override
-    public String[] uploadFileV1(MultipartFile file) throws Exception {
-        return new String[0];
-    }
 }

+ 8 - 8
siwei-modules/siwei-file/src/main/java/com/siwei/file/service/ISysFileService.java

@@ -17,12 +17,12 @@ public interface ISysFileService {
      */
     public String uploadFile(MultipartFile file) throws Exception;
 
-    /**
-     * 文件上传接口v1
-     *
-     * @param file 上传的文件
-     * @return 访问地址, 和相对地址
-     * @throws Exception
-     */
-    public String[] uploadFileV1(MultipartFile file) throws Exception;
+//    /**
+//     * 文件上传接口v1
+//     *
+//     * @param file 上传的文件
+//     * @return 访问地址, 和相对地址
+//     * @throws Exception
+//     */
+//    public String[] uploadFileV1(MultipartFile file) throws Exception;
 }

+ 2 - 28
siwei-modules/siwei-file/src/main/java/com/siwei/file/service/LocalSysFileServiceImpl.java

@@ -14,18 +14,6 @@ import com.siwei.file.utils.FileUploadUtils;
 @Primary
 @Service
 public class LocalSysFileServiceImpl implements ISysFileService {
-    /**
-     * 资源映射路径 前缀
-     */
-    @Value("${file.prefix}")
-    public String localFilePrefix;
-
-    /**
-     * 域名或本机访问地址
-     */
-    @Value("${file.domain}")
-    public String domain;
-
     /**
      * 上传文件存储在本地的根路径
      */
@@ -42,21 +30,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
     @Override
     public String uploadFile(MultipartFile file) throws Exception {
         String name = FileUploadUtils.upload(localFilePath, file);
-        String url = domain + localFilePrefix + name;
-        return url;
-    }
-
-    /**
-     * 本地文件上传接口
-     *
-     * @param file 上传的文件
-     * @return 访问地址, 相对地址
-     * @throws Exception
-     */
-    @Override
-    public String[] uploadFileV1(MultipartFile file) throws Exception {
-        String name = FileUploadUtils.upload(localFilePath, file);
-        String url = domain + localFilePrefix + name;
-        return new String[]{url, name};
+        String path = localFilePath + name;
+        return path;
     }
 }

+ 1 - 4
siwei-modules/siwei-file/src/main/java/com/siwei/file/service/MinioSysFileServiceImpl.java

@@ -59,8 +59,5 @@ public class MinioSysFileServiceImpl implements ISysFileService
         }
     }
 
-    @Override
-    public String[] uploadFileV1(MultipartFile file) throws Exception {
-        return new String[0];
-    }
+
 }

+ 3 - 3
siwei-modules/siwei-file/src/main/resources/bootstrap.yml

@@ -6,7 +6,7 @@ server:
 spring: 
   application:
     # 应用名称
-    name: siwei-file
+    name: file
   profiles:
     # 环境配置
     active: dev
@@ -15,14 +15,14 @@ spring:
       discovery:
         # 服务注册地址
         enabled: true
-        namespace: siwei
+        namespace: one_code
         server-addr: 127.0.0.1:8848
         username: nacos
         password: nacos
       config:
         # 配置中心地址
         enabled: true
-        namespace: siwei
+        namespace: one_code
         server-addr: 127.0.0.1:8848
         username: nacos
         password: nacos