Просмотр исходного кода

增加分布式文件FastDFS支持

RuoYi лет назад: 5
Родитель
Сommit
2bb0eb1edb

+ 57 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileUtils.java

@@ -20,6 +20,12 @@ import com.ruoyi.common.core.utils.StringUtils;
20
  */
20
  */
21
 public class FileUtils extends org.apache.commons.io.FileUtils
21
 public class FileUtils extends org.apache.commons.io.FileUtils
22
 {
22
 {
23
+    /** 字符常量:斜杠 {@code '/'} */
24
+    public static final char SLASH = '/';
25
+
26
+    /** 字符常量:反斜杠 {@code '\\'} */
27
+    public static final char BACKSLASH = '\\';
28
+
23
     public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
29
     public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
24
 
30
 
25
     /**
31
     /**
@@ -168,6 +174,57 @@ public class FileUtils extends org.apache.commons.io.FileUtils
168
     }
174
     }
169
 
175
 
170
     /**
176
     /**
177
+     * 返回文件名
178
+     *
179
+     * @param filePath 文件
180
+     * @return 文件名
181
+     */
182
+    public static String getName(String filePath)
183
+    {
184
+        if (null == filePath)
185
+        {
186
+            return null;
187
+        }
188
+        int len = filePath.length();
189
+        if (0 == len)
190
+        {
191
+            return filePath;
192
+        }
193
+        if (isFileSeparator(filePath.charAt(len - 1)))
194
+        {
195
+            // 以分隔符结尾的去掉结尾分隔符
196
+            len--;
197
+        }
198
+
199
+        int begin = 0;
200
+        char c;
201
+        for (int i = len - 1; i > -1; i--)
202
+        {
203
+            c = filePath.charAt(i);
204
+            if (isFileSeparator(c))
205
+            {
206
+                // 查找最后一个路径分隔符(/或者\)
207
+                begin = i + 1;
208
+                break;
209
+            }
210
+        }
211
+
212
+        return filePath.substring(begin, len);
213
+    }
214
+
215
+    /**
216
+     * 是否为Windows或者Linux(Unix)文件分隔符<br>
217
+     * Windows平台下分隔符为\,Linux(Unix)为/
218
+     *
219
+     * @param c 字符
220
+     * @return 是否为Windows或者Linux(Unix)文件分隔符
221
+     */
222
+    public static boolean isFileSeparator(char c)
223
+    {
224
+        return SLASH == c || BACKSLASH == c;
225
+    }
226
+
227
+    /**
171
      * 下载文件名重新编码
228
      * 下载文件名重新编码
172
      *
229
      *
173
      * @param response 响应对象
230
      * @param response 响应对象

+ 7 - 0
ruoyi-modules/ruoyi-file/pom.xml

@@ -48,6 +48,13 @@
48
             <version>${swagger.fox.version}</version>
48
             <version>${swagger.fox.version}</version>
49
         </dependency>
49
         </dependency>
50
         
50
         
51
+        <!-- FastDFS -->
52
+        <dependency>
53
+            <groupId>com.github.tobato</groupId>
54
+            <artifactId>fastdfs-client</artifactId>
55
+            <version>1.26.5</version>
56
+        </dependency>
57
+        
51
         <!-- Ruoyi Common Security -->
58
         <!-- Ruoyi Common Security -->
52
         <dependency>
59
         <dependency>
53
             <groupId>com.ruoyi</groupId>
60
             <groupId>com.ruoyi</groupId>

+ 4 - 16
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/controller/SysFileController.java

@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.PostMapping;
8
 import org.springframework.web.bind.annotation.RestController;
8
 import org.springframework.web.bind.annotation.RestController;
9
 import org.springframework.web.multipart.MultipartFile;
9
 import org.springframework.web.multipart.MultipartFile;
10
 import com.ruoyi.common.core.domain.R;
10
 import com.ruoyi.common.core.domain.R;
11
+import com.ruoyi.common.core.utils.file.FileUtils;
11
 import com.ruoyi.file.service.ISysFileService;
12
 import com.ruoyi.file.service.ISysFileService;
12
 import com.ruoyi.system.api.domain.SysFile;
13
 import com.ruoyi.system.api.domain.SysFile;
13
 
14
 
@@ -27,18 +28,6 @@ public class SysFileController
27
     @Value("${file.path}")
28
     @Value("${file.path}")
28
     private String localFilePath;
29
     private String localFilePath;
29
 
30
 
30
-    /**
31
-     * 资源映射路径 前缀
32
-     */
33
-    @Value("${file.prefix}")
34
-    public String localFilePrefix;
35
-
36
-    /**
37
-     * 域名或本机访问地址
38
-     */
39
-    @Value("${file.domain}")
40
-    public String domain;
41
-
42
     @Autowired
31
     @Autowired
43
     private ISysFileService sysFileService;
32
     private ISysFileService sysFileService;
44
 
33
 
@@ -50,11 +39,10 @@ public class SysFileController
50
     {
39
     {
51
         try
40
         try
52
         {
41
         {
53
-            // 上传并返回新文件名称
54
-            String fileName = sysFileService.uploadFile(file, localFilePath);
55
-            String url = domain + localFilePrefix + fileName;
42
+            // 上传并返回访问地址
43
+            String url = sysFileService.uploadFile(file, localFilePath);
56
             SysFile sysFile = new SysFile();
44
             SysFile sysFile = new SysFile();
57
-            sysFile.setName(fileName);
45
+            sysFile.setName(FileUtils.getName(url));
58
             sysFile.setUrl(url);
46
             sysFile.setUrl(url);
59
             return R.ok(sysFile);
47
             return R.ok(sysFile);
60
         }
48
         }

+ 43 - 0
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java

@@ -0,0 +1,43 @@
1
+package com.ruoyi.file.service;
2
+
3
+import org.apache.commons.io.FilenameUtils;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.beans.factory.annotation.Value;
6
+import org.springframework.stereotype.Service;
7
+import org.springframework.web.multipart.MultipartFile;
8
+import com.github.tobato.fastdfs.domain.fdfs.StorePath;
9
+import com.github.tobato.fastdfs.service.FastFileStorageClient;
10
+
11
+/**
12
+ * FastDFS文件存储
13
+ * 
14
+ * @author ruoyi
15
+ */
16
+@Service
17
+public class FastDfsSysFileServiceImpl implements ISysFileService
18
+{
19
+    /**
20
+     * 域名或本机访问地址
21
+     */
22
+    @Value("${fdfs.domain}")
23
+    public String domain;
24
+
25
+    @Autowired
26
+    private FastFileStorageClient storageClient;
27
+
28
+    /**
29
+     * FastDfs文件上传接口
30
+     * 
31
+     * @param file 上传的文件
32
+     * @param baseDir 相对应用的基目录
33
+     * @return 访问地址
34
+     * @throws Exception
35
+     */
36
+    @Override
37
+    public String uploadFile(MultipartFile file, String baseDir) throws Exception
38
+    {
39
+        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
40
+                FilenameUtils.getExtension(file.getOriginalFilename()), null);
41
+        return domain + "/" + storePath.getFullPath();
42
+    }
43
+}

+ 1 - 1
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/ISysFileService.java

@@ -14,7 +14,7 @@ public interface ISysFileService
14
      * 
14
      * 
15
      * @param file 上传的文件
15
      * @param file 上传的文件
16
      * @param baseDir 相对应用的基目录
16
      * @param baseDir 相对应用的基目录
17
-     * @return 文件名称
17
+     * @return 访问地址
18
      * @throws Exception
18
      * @throws Exception
19
      */
19
      */
20
     public String uploadFile(MultipartFile file, String baseDir) throws Exception;
20
     public String uploadFile(MultipartFile file, String baseDir) throws Exception;

+ 20 - 3
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/LocalSysFileServiceImpl.java

@@ -1,5 +1,7 @@
1
 package com.ruoyi.file.service;
1
 package com.ruoyi.file.service;
2
 
2
 
3
+import org.springframework.beans.factory.annotation.Value;
4
+import org.springframework.context.annotation.Primary;
3
 import org.springframework.stereotype.Service;
5
 import org.springframework.stereotype.Service;
4
 import org.springframework.web.multipart.MultipartFile;
6
 import org.springframework.web.multipart.MultipartFile;
5
 import com.ruoyi.file.utils.FileUploadUtils;
7
 import com.ruoyi.file.utils.FileUploadUtils;
@@ -9,19 +11,34 @@ import com.ruoyi.file.utils.FileUploadUtils;
9
  * 
11
  * 
10
  * @author ruoyi
12
  * @author ruoyi
11
  */
13
  */
14
+@Primary
12
 @Service
15
 @Service
13
 public class LocalSysFileServiceImpl implements ISysFileService
16
 public class LocalSysFileServiceImpl implements ISysFileService
14
 {
17
 {
15
     /**
18
     /**
16
-     * 文件上传接口
19
+     * 资源映射路径 前缀
20
+     */
21
+    @Value("${file.prefix}")
22
+    public String localFilePrefix;
23
+
24
+    /**
25
+     * 域名或本机访问地址
26
+     */
27
+    @Value("${file.domain}")
28
+    public String domain;
29
+
30
+    /**
31
+     * 本地文件上传接口
17
      * 
32
      * 
18
      * @param file 上传的文件
33
      * @param file 上传的文件
19
      * @param baseDir 相对应用的基目录
34
      * @param baseDir 相对应用的基目录
20
-     * @return 文件名称
35
+     * @return 访问地址
21
      * @throws Exception
36
      * @throws Exception
22
      */
37
      */
23
     public String uploadFile(MultipartFile file, String baseDir) throws Exception
38
     public String uploadFile(MultipartFile file, String baseDir) throws Exception
24
     {
39
     {
25
-        return FileUploadUtils.upload(baseDir, file);
40
+        String name = FileUploadUtils.upload(baseDir, file);
41
+        String url = domain + localFilePrefix + name;
42
+        return url;
26
     }
43
     }
27
 }
44
 }