吉安感知网项目-后端
linwei
5 days ago aba829f97bc8c0c7828727676edf4a2c2bd0b282
fix(oss): 解决文件上传流重置异常问题

- 在 AttachClient 中将 MultipartFile 转换为字节数组避免流重置异常
- 在 OssEndpoint 中将 MultipartFile 转换为字节数组避免流重置异常
- 使用 ByteArrayInputStream 替代原始输入流进行文件上传
- 添加详细的步骤注释说明文件转换过程
- 保持原有的文件上传功能逻辑不变
2 files modified
39 ■■■■ changed files
drone-ops/drone-resource/src/main/java/org/sxkj/resource/endpoint/OssEndpoint.java 23 ●●●● patch | view | raw | blame | history
drone-ops/drone-resource/src/main/java/org/sxkj/resource/feign/AttachClient.java 16 ●●●● patch | view | raw | blame | history
drone-ops/drone-resource/src/main/java/org/sxkj/resource/endpoint/OssEndpoint.java
@@ -21,6 +21,7 @@
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;
@@ -144,7 +145,10 @@
    @PostMapping("/put-file")
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public R<BladeFile> putFile(@RequestParam MultipartFile file) {
        BladeFile bladeFile = ossBuilder.template("estack").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);
    }
@@ -159,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);
    }
@@ -191,7 +198,10 @@
    public R<BladeFile> putFileAttach(@RequestParam String fileName,
                                      @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);
@@ -223,8 +233,13 @@
    @NotNull
    private BladeFile getBladeFile(@RequestParam MultipartFile file) throws IOException {
        // 步骤1: 获取文件名
        String fileName = file.getOriginalFilename();
        BladeFile bladeFile = ossBuilder.template("estack").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;
drone-ops/drone-resource/src/main/java/org/sxkj/resource/feign/AttachClient.java
@@ -26,6 +26,7 @@
import org.sxkj.resource.vo.AttachTypeStatisticsVO;
import org.sxkj.resource.vo.AttachVO;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -121,7 +122,10 @@
    }
    @Override
    public BladeFile saveAttachFile(MultipartFile file, String fileName) throws IOException {
        BladeFile bladeFile = ossBuilder.template().putFile(pojo.getBucket(),fileName, file.getInputStream());
        // 步骤1: 将文件转换为字节数组,避免流重置异常
        byte[] bytes = file.getBytes();
        // 步骤2: 使用字节数组输入流上传文件
        BladeFile bladeFile = ossBuilder.template().putFile(pojo.getBucket(),fileName, new ByteArrayInputStream(bytes));
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile, null);
        bladeFile.setAttachId(attachId);
        return bladeFile;
@@ -129,7 +133,10 @@
    @Override
    public BladeFile saveAttachFile(MultipartFile file, String fileName, List<List<String>> type) throws IOException {
        BladeFile bladeFile = ossBuilder.template().putFile(pojo.getBucket(),fileName, file.getInputStream());
        // 步骤1: 将文件转换为字节数组,避免流重置异常
        byte[] bytes = file.getBytes();
        // 步骤2: 使用字节数组输入流上传文件
        BladeFile bladeFile = ossBuilder.template().putFile(pojo.getBucket(),fileName, new ByteArrayInputStream(bytes));
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile, type);
        bladeFile.setAttachId(attachId);
        return bladeFile;
@@ -144,7 +151,10 @@
     */
    @Override
    public BladeFile putFile(MultipartFile file, String fileName) throws IOException {
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
        // 步骤1: 将文件转换为字节数组,避免流重置异常
        byte[] bytes = file.getBytes();
        // 步骤2: 使用字节数组输入流上传文件
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, new ByteArrayInputStream(bytes));
        return bladeFile;
    }