From 27cd98f0db6b90fb925f556f8318d998828eaf63 Mon Sep 17 00:00:00 2001 From: shahaibo <1023316923@qq.com> Date: Mon, 7 Apr 2025 17:12:33 +0800 Subject: [PATCH] =?UTF-8?q?TASK:=E6=A8=A1=E5=9D=97=E5=8C=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ai/da/common/utils/MinioUtil.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ai/da/common/utils/MinioUtil.java b/src/main/java/com/ai/da/common/utils/MinioUtil.java index 6e9dd42e..d67c9ab8 100644 --- a/src/main/java/com/ai/da/common/utils/MinioUtil.java +++ b/src/main/java/com/ai/da/common/utils/MinioUtil.java @@ -374,14 +374,31 @@ public class MinioUtil { */ public String getPreSignedUrl(String bucketName, String fileName, int expiry) { try { - return minioClient.getPresignedObjectUrl( - GetPresignedObjectUrlArgs.builder() - .bucket(bucketName) - .object(fileName) - .expiry(expiry, TimeUnit.MINUTES) - .method(Method.GET) - .build() - ); + + String lowerName = fileName.toLowerCase(); + boolean isImage = lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") || lowerName.endsWith(".png"); + + GetPresignedObjectUrlArgs.Builder builder = GetPresignedObjectUrlArgs.builder() + .bucket(bucketName) + .object(fileName) + .expiry(expiry, TimeUnit.MINUTES) + .method(Method.GET); + + if (isImage) { + // 根据后缀名设置正确的 content-type + String contentType = "image/jpeg"; + if (lowerName.endsWith(".png")) { + contentType = "image/png"; + } + + Map queryParams = new HashMap<>(); + queryParams.put("response-content-type", contentType); + queryParams.put("response-content-disposition", "inline"); + + builder.extraQueryParams(queryParams); + } + + return minioClient.getPresignedObjectUrl(builder.build()); } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) { e.printStackTrace(); throw new BusinessException(e.getMessage());