Browse Source

!302 【轻量级 PR】 文件上传服务获取InputStream未关闭
Merge pull request !302 from 程序凡/master

若依 3 years ago
parent
commit
26c63520f2

+ 7 - 3
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java

@@ -8,9 +8,11 @@ import com.github.tobato.fastdfs.domain.fdfs.StorePath;
8
 import com.github.tobato.fastdfs.service.FastFileStorageClient;
8
 import com.github.tobato.fastdfs.service.FastFileStorageClient;
9
 import com.ruoyi.common.core.utils.file.FileTypeUtils;
9
 import com.ruoyi.common.core.utils.file.FileTypeUtils;
10
 
10
 
11
+import java.io.InputStream;
12
+
11
 /**
13
 /**
12
  * FastDFS 文件存储
14
  * FastDFS 文件存储
13
- * 
15
+ *
14
  * @author ruoyi
16
  * @author ruoyi
15
  */
17
  */
16
 @Service
18
 @Service
@@ -27,7 +29,7 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
27
 
29
 
28
     /**
30
     /**
29
      * FastDfs文件上传接口
31
      * FastDfs文件上传接口
30
-     * 
32
+     *
31
      * @param file 上传的文件
33
      * @param file 上传的文件
32
      * @return 访问地址
34
      * @return 访问地址
33
      * @throws Exception
35
      * @throws Exception
@@ -35,8 +37,10 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
35
     @Override
37
     @Override
36
     public String uploadFile(MultipartFile file) throws Exception
38
     public String uploadFile(MultipartFile file) throws Exception
37
     {
39
     {
38
-        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
40
+        InputStream inputStream = file.getInputStream();
41
+        StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(),
39
                 FileTypeUtils.getExtension(file), null);
42
                 FileTypeUtils.getExtension(file), null);
43
+        inputStream.close();
40
         return domain + "/" + storePath.getFullPath();
44
         return domain + "/" + storePath.getFullPath();
41
     }
45
     }
42
 }
46
 }

+ 8 - 4
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/MinioSysFileServiceImpl.java

@@ -8,9 +8,11 @@ import com.ruoyi.file.utils.FileUploadUtils;
8
 import io.minio.MinioClient;
8
 import io.minio.MinioClient;
9
 import io.minio.PutObjectArgs;
9
 import io.minio.PutObjectArgs;
10
 
10
 
11
+import java.io.InputStream;
12
+
11
 /**
13
 /**
12
  * Minio 文件存储
14
  * Minio 文件存储
13
- * 
15
+ *
14
  * @author ruoyi
16
  * @author ruoyi
15
  */
17
  */
16
 @Service
18
 @Service
@@ -23,8 +25,8 @@ public class MinioSysFileServiceImpl implements ISysFileService
23
     private MinioClient client;
25
     private MinioClient client;
24
 
26
 
25
     /**
27
     /**
26
-     * 本地文件上传接口
27
-     * 
28
+     * Minio文件上传接口
29
+     *
28
      * @param file 上传的文件
30
      * @param file 上传的文件
29
      * @return 访问地址
31
      * @return 访问地址
30
      * @throws Exception
32
      * @throws Exception
@@ -33,13 +35,15 @@ public class MinioSysFileServiceImpl implements ISysFileService
33
     public String uploadFile(MultipartFile file) throws Exception
35
     public String uploadFile(MultipartFile file) throws Exception
34
     {
36
     {
35
         String fileName = FileUploadUtils.extractFilename(file);
37
         String fileName = FileUploadUtils.extractFilename(file);
38
+        InputStream inputStream = file.getInputStream();
36
         PutObjectArgs args = PutObjectArgs.builder()
39
         PutObjectArgs args = PutObjectArgs.builder()
37
                 .bucket(minioConfig.getBucketName())
40
                 .bucket(minioConfig.getBucketName())
38
                 .object(fileName)
41
                 .object(fileName)
39
-                .stream(file.getInputStream(), file.getSize(), -1)
42
+                .stream(inputStream, file.getSize(), -1)
40
                 .contentType(file.getContentType())
43
                 .contentType(file.getContentType())
41
                 .build();
44
                 .build();
42
         client.putObject(args);
45
         client.putObject(args);
46
+        inputStream.close();
43
         return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
47
         return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
44
     }
48
     }
45
 }
49
 }