From 92c6d218554b028a8085514eeb18f1dfcd41af58 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Thu, 05 Dec 2024 14:27:54 +0800
Subject: [PATCH] 优化文件异常输入流未关闭的问题
---
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java | 24 +++++++++++++++++++-----
1 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java
index 471b40b..da0dc60 100644
--- a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java
+++ b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java
@@ -1,5 +1,7 @@
package com.ruoyi.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;
import org.springframework.stereotype.Service;
@@ -10,7 +12,7 @@
/**
* FastDFS 文件存储
- *
+ *
* @author ruoyi
*/
@Service
@@ -27,7 +29,7 @@
/**
* FastDfs文件上传接口
- *
+ *
* @param file 上传的文件
* @return 访问地址
* @throws Exception
@@ -35,8 +37,20 @@
@Override
public String uploadFile(MultipartFile file) throws Exception
{
- StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
- FileTypeUtils.getExtension(file), null);
- return domain + "/" + storePath.getFullPath();
+ InputStream inputStream = null;
+ try
+ {
+ inputStream = file.getInputStream();
+ StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FileTypeUtils.getExtension(file), null);
+ return domain + "/" + storePath.getFullPath();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("FastDfs Failed to upload file", e);
+ }
+ finally
+ {
+ IoUtils.closeQuietly(inputStream);
+ }
}
}
--
Gitblit v1.9.3