From d9d9765a4ab36230bb9a069f66b2ead0a67e57e7 Mon Sep 17 00:00:00 2001 From: AprilWind <2100166581@qq.com> Date: Thu, 23 May 2024 09:53:06 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87=E9=A2=84=E8=A7=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/common/oss/core/OssClient.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 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 fcad35b0..86668aa2 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 @@ -162,13 +162,14 @@ public class OssClient { /** * 上传文件到 Amazon S3,并返回上传结果 * - * @param filePath 本地文件路径 - * @param key 在 Amazon S3 中的对象键 - * @param md5Digest 本地文件的 MD5 哈希值(可选) + * @param filePath 本地文件路径 + * @param key 在 Amazon S3 中的对象键 + * @param md5Digest 本地文件的 MD5 哈希值(可选) + * @param contentType 文件内容类型 * @return UploadResult 包含上传后的文件信息 * @throws OssException 如果上传失败,抛出自定义异常 */ - public UploadResult upload(Path filePath, String key, String md5Digest) { + public UploadResult upload(Path filePath, String key, String md5Digest, String contentType) { try { // 构建上传请求对象 FileUpload fileUpload = transferManager.uploadFile( @@ -176,6 +177,7 @@ public class OssClient { y -> y.bucket(properties.getBucketName()) .key(key) .contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null) + .contentType(contentType) .build()) .addTransferListener(LoggingTransferListener.create()) .source(filePath).build()); @@ -201,10 +203,11 @@ public class OssClient { * @param inputStream 要上传的输入流 * @param key 在 Amazon S3 中的对象键 * @param length 输入流的长度 + * @param contentType 文件内容类型 * @return UploadResult 包含上传后的文件信息 * @throws OssException 如果上传失败,抛出自定义异常 */ - public UploadResult upload(InputStream inputStream, String key, Long length) { + public UploadResult upload(InputStream inputStream, String key, Long length, String contentType) { // 如果输入流不是 ByteArrayInputStream,则将其读取为字节数组再创建 ByteArrayInputStream if (!(inputStream instanceof ByteArrayInputStream)) { inputStream = new ByteArrayInputStream(IoUtil.readBytes(inputStream)); @@ -219,6 +222,7 @@ public class OssClient { .putObjectRequest( y -> y.bucket(properties.getBucketName()) .key(key) + .contentType(contentType) .build()) .build()); @@ -335,7 +339,7 @@ public class OssClient { * @throws OssException 如果上传失败,抛出自定义异常 */ public UploadResult uploadSuffix(byte[] data, String suffix) { - return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length)); + return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length), FileUtils.getMimeType(suffix)); } /** @@ -348,7 +352,7 @@ public class OssClient { * @throws OssException 如果上传失败,抛出自定义异常 */ public UploadResult uploadSuffix(InputStream inputStream, String suffix, Long length) { - return upload(inputStream, getPath(properties.getPrefix(), suffix), length); + return upload(inputStream, getPath(properties.getPrefix(), suffix), length, FileUtils.getMimeType(suffix)); } /** @@ -360,7 +364,7 @@ public class OssClient { * @throws OssException 如果上传失败,抛出自定义异常 */ public UploadResult uploadSuffix(File file, String suffix) { - return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null); + return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null, FileUtils.getMimeType(suffix)); } /**