From ae3be9b48d950e92835e72a44c9c5e6263fcf65c Mon Sep 17 00:00:00 2001 From: yys <47@gamerwa.com> Date: Tue, 7 Apr 2026 13:56:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=20=E4=BC=98=E5=8C=96=E8=85=BE=E8=AE=AF?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E6=A1=B6=E9=80=BB=E8=BE=91=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=AF=8F=E6=AC=A1=E9=83=BD=E5=BC=80=E5=85=B3=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/utils/TencentCosUtil.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/web-api/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentCosUtil.java b/web-api/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentCosUtil.java index 1612c9e..5a27fde 100644 --- a/web-api/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentCosUtil.java +++ b/web-api/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentCosUtil.java @@ -13,6 +13,9 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -38,6 +41,19 @@ public class TencentCosUtil { @Value("${tencentCos.domain}") private String domain; + private COSClient cosClient; + + @PostConstruct + public void initCosClient() { + cosClient = createCosClient(); + } + + @PreDestroy + public void destroyCosClient() { + if (cosClient != null) { + cosClient.shutdown(); + } + } /** * 上传MultipartFile到腾讯云COS,返回文件访问地址 @@ -59,8 +75,6 @@ public class TencentCosUtil { throw new IllegalArgumentException("上传文件不能为空"); } - COSClient cosClient = createCosClient(); - // 生成唯一文件键,格式与AWS一致:yyyy/MM/dd/uuid_filename String key = generateCosKey(file.getOriginalFilename()); @@ -89,8 +103,6 @@ public class TencentCosUtil { } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("上传文件到COS失败: " + e.getMessage(), e); - } finally { - cosClient.shutdown(); } }