From 17c3c7e8c2e1efaa0406c105f9324aef74d4365e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=89=9B=E5=AD=90Li?= Date: Fri, 17 Jan 2025 16:14:39 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=97=B6ContentLength=E6=9C=AA=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E8=AE=BE=E7=BD=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/dromara/common/oss/core/OssClient.java | 10 +++++++--- .../dromara/system/service/impl/SysOssServiceImpl.java | 5 ++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java index 59c599bd0..70c001ad9 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java @@ -2,6 +2,7 @@ package org.dromara.common.oss.core; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.IdUtil; +import jakarta.servlet.http.HttpServletResponse; import org.dromara.common.core.constant.Constants; import org.dromara.common.core.utils.DateUtils; import org.dromara.common.core.utils.StringUtils; @@ -238,11 +239,11 @@ public class OssClient { * 下载文件从 Amazon S3 到 输出流 * * @param key 文件在 Amazon S3 中的对象键 - * @param out 输出流 + * @param response 响应头 * @return 输出流中写入的字节数(长度) * @throws OssException 如果下载失败,抛出自定义异常 */ - public long download(String key, OutputStream out) { + public long download(String key, HttpServletResponse response) { try { // 构建下载请求 DownloadRequest> downloadRequest = DownloadRequest.builder() @@ -258,7 +259,10 @@ public class OssClient { Download> responseFuture = transferManager.download(downloadRequest); // 输出到流中 try (ResponseInputStream responseStream = responseFuture.completionFuture().join().result()) { // auto-closeable stream - return responseStream.transferTo(out); // 阻塞调用线程 blocks the calling thread + GetObjectResponse objectResponse = responseStream.response(); + // 设置文件大小到响应头(确保在传输文件之前设置 Content-Length) + response.setContentLengthLong(objectResponse.contentLength()); + return responseStream.transferTo(response.getOutputStream()); // 阻塞调用线程 blocks the calling thread } } catch (Exception e) { throw new OssException("文件下载失败,错误信息:[" + e.getMessage() + "]"); diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssServiceImpl.java index 496113742..bcada74d2 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssServiceImpl.java @@ -169,7 +169,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService { * @param response HttpServletResponse对象,用于设置响应头和向客户端发送文件内容 */ @Override - public void download(Long ossId, HttpServletResponse response) throws IOException { + public void download(Long ossId, HttpServletResponse response) { SysOssVo sysOss = SpringUtils.getAopProxy(this).getById(ossId); if (ObjectUtil.isNull(sysOss)) { throw new ServiceException("文件数据不存在!"); @@ -177,8 +177,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService { FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); OssClient storage = OssFactory.instance(sysOss.getService()); - long contentLength = storage.download(sysOss.getFileName(), response.getOutputStream()); - response.setContentLengthLong(contentLength); + storage.download(sysOss.getFileName(), response); } /**