From aba829f97bc8c0c7828727676edf4a2c2bd0b282 Mon Sep 17 00:00:00 2001
From: linwei <872216696@qq.com>
Date: Fri, 31 Jul 2026 14:40:33 +0800
Subject: [PATCH] fix(oss): 解决文件上传流重置异常问题

---
 drone-ops/drone-resource/src/main/java/org/sxkj/resource/endpoint/OssEndpoint.java |   28 ++++++++++++++++++++++------
 1 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/drone-ops/drone-resource/src/main/java/org/sxkj/resource/endpoint/OssEndpoint.java b/drone-ops/drone-resource/src/main/java/org/sxkj/resource/endpoint/OssEndpoint.java
index 25f9640..3278de8 100644
--- a/drone-ops/drone-resource/src/main/java/org/sxkj/resource/endpoint/OssEndpoint.java
+++ b/drone-ops/drone-resource/src/main/java/org/sxkj/resource/endpoint/OssEndpoint.java
@@ -21,8 +21,10 @@
 import org.sxkj.resource.util.ImageCompressorUtil;
 import springfox.documentation.annotations.ApiIgnore;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -143,7 +145,10 @@
     @PostMapping("/put-file")
     @ApiOperation(value = "上传文件", notes = "上传文件")
     public R<BladeFile> putFile(@RequestParam MultipartFile file) {
-        BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
+        // 步骤1: 将文件转换为字节数组,避免流重置异常
+        byte[] bytes = file.getBytes();
+        // 步骤2: 使用字节数组输入流上传文件
+        BladeFile bladeFile = ossBuilder.template("estack").putFile(file.getOriginalFilename(), new ByteArrayInputStream(bytes));
         return R.data(bladeFile);
     }
 
@@ -158,7 +163,10 @@
     @PostMapping("/put-file-by-name")
     @ApiOperation(value = "上传文件", notes = "上传文件-存储桶对象名称")
     public R<BladeFile> putFile(@RequestParam String fileName, @RequestParam MultipartFile file) {
-        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
+        // 步骤1: 将文件转换为字节数组,避免流重置异常
+        byte[] bytes = file.getBytes();
+        // 步骤2: 使用字节数组输入流上传文件
+        BladeFile bladeFile = ossBuilder.template().putFile(fileName, new ByteArrayInputStream(bytes));
         return R.data(bladeFile);
     }
 
@@ -188,9 +196,12 @@
     @PostMapping("/put-file-attach-by-name")
     @ApiOperation(value = "上传文件并保存至附件表", notes = "上传文件并保存至附件表-存储桶对象名称")
     public R<BladeFile> putFileAttach(@RequestParam String fileName,
-                                      @RequestParam Integer resultType,
+                                      @RequestParam List<List<String>> resultType,
                                       @RequestParam MultipartFile file) {
-        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
+        // 步骤1: 将文件转换为字节数组,避免流重置异常
+        byte[] bytes = file.getBytes();
+        // 步骤2: 使用字节数组输入流上传文件
+        BladeFile bladeFile = ossBuilder.template().putFile(fileName, new ByteArrayInputStream(bytes));
         Long attachId = buildAttach(fileName, resultType, file.getSize(), bladeFile);
         bladeFile.setAttachId(attachId);
         return R.data(bladeFile);
@@ -222,8 +233,13 @@
 
     @NotNull
     private BladeFile getBladeFile(@RequestParam MultipartFile file) throws IOException {
+        // 步骤1: 获取文件名
         String fileName = file.getOriginalFilename();
-        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
+        // 步骤2: 将文件转换为字节数组,避免流重置异常
+        byte[] bytes = file.getBytes();
+        // 步骤3: 使用字节数组输入流上传文件
+        BladeFile bladeFile = ossBuilder.template("estack").putFile(fileName, new ByteArrayInputStream(bytes));
+        // 步骤4: 构建附件信息
         Long attachId = buildAttach(fileName, null, file.getSize(), bladeFile);
         bladeFile.setAttachId(attachId);
         return bladeFile;
@@ -237,7 +253,7 @@
      * @param bladeFile 对象存储文件
      * @return attachId
      */
-    private Long buildAttach(String fileName, Integer resultType, Long fileSize, BladeFile bladeFile) {
+    private Long buildAttach(String fileName, List<List<String>> resultType, Long fileSize, BladeFile bladeFile) {
         String fileExtension = FileUtil.getFileExtension(fileName);
         Attach attach = new Attach();
         attach.setDomainUrl(bladeFile.getDomain());

--
Gitblit v1.9.3