Преглед на файлове

优化文件异常输入流未关闭的问题

RuoYi преди 1 година
родител
ревизия
92c6d21855

+ 15 - 5
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java

@@ -37,10 +37,20 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
37 37
     @Override
38 38
     public String uploadFile(MultipartFile file) throws Exception
39 39
     {
40
-        InputStream inputStream = file.getInputStream();
41
-        StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(),
42
-                FileTypeUtils.getExtension(file), null);
43
-        IoUtils.closeQuietly(inputStream);
44
-        return domain + "/" + storePath.getFullPath();
40
+        InputStream inputStream = null;
41
+        try
42
+        {
43
+            inputStream = file.getInputStream();
44
+            StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FileTypeUtils.getExtension(file), null);
45
+            return domain + "/" + storePath.getFullPath();
46
+        }
47
+        catch (Exception e)
48
+        {
49
+            throw new RuntimeException("FastDfs Failed to upload file", e);
50
+        }
51
+        finally
52
+        {
53
+            IoUtils.closeQuietly(inputStream);
54
+        }
45 55
     }
46 56
 }

+ 22 - 11
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/MinioSysFileServiceImpl.java

@@ -34,16 +34,27 @@ public class MinioSysFileServiceImpl implements ISysFileService
34 34
     @Override
35 35
     public String uploadFile(MultipartFile file) throws Exception
36 36
     {
37
-        String fileName = FileUploadUtils.extractFilename(file);
38
-        InputStream inputStream = file.getInputStream();
39
-        PutObjectArgs args = PutObjectArgs.builder()
40
-                .bucket(minioConfig.getBucketName())
41
-                .object(fileName)
42
-                .stream(inputStream, file.getSize(), -1)
43
-                .contentType(file.getContentType())
44
-                .build();
45
-        client.putObject(args);
46
-        IoUtils.closeQuietly(inputStream);
47
-        return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
37
+        InputStream inputStream = null;
38
+        try
39
+        {
40
+            String fileName = FileUploadUtils.extractFilename(file);
41
+            inputStream = file.getInputStream();
42
+            PutObjectArgs args = PutObjectArgs.builder()
43
+                    .bucket(minioConfig.getBucketName())
44
+                    .object(fileName)
45
+                    .stream(inputStream, file.getSize(), -1)
46
+                    .contentType(file.getContentType())
47
+                    .build();
48
+            client.putObject(args);
49
+            return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
50
+        }
51
+        catch (Exception e)
52
+        {
53
+            throw new RuntimeException("Minio Failed to upload file", e);
54
+        }
55
+        finally
56
+        {
57
+            IoUtils.closeQuietly(inputStream);
58
+        }
48 59
     }
49 60
 }