From 27b3ffa1dcf89f4dc5fdd13a96f7c985d0eba68d Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Thu, 30 Oct 2025 11:19:00 +0800
Subject: [PATCH] oss增加按【通过文件扩展名获取MIME类型】存储对象
---
ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java | 37 ++++++++++++++++++
ard-oss/src/main/java/com/ard/oss/service/impl/AmazonS3ServiceImpl.java | 6 +++
ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java | 39 ++++++++++++++++++-
ard-oss/src/main/java/com/ard/oss/service/impl/AliyunOssServiceImpl.java | 5 ++
ard-oss/src/main/java/com/ard/oss/service/IOssService.java | 7 +++
5 files changed, 92 insertions(+), 2 deletions(-)
diff --git a/ard-oss/src/main/java/com/ard/oss/service/IOssService.java b/ard-oss/src/main/java/com/ard/oss/service/IOssService.java
index c2d8224..27c916a 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/IOssService.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/IOssService.java
@@ -1,6 +1,7 @@
package com.ard.oss.service;
import com.ard.oss.model.CredentialsDTO;
+
import java.io.InputStream;
import java.net.URL;
@@ -15,12 +16,14 @@
/**
* Get temporary credentials.
+ *
* @return
*/
CredentialsDTO getCredentials();
/**
* Get the address of the object based on the bucket name and the object name.
+ *
* @param bucket bucket name
* @param objectKey object name
* @return download link
@@ -29,6 +32,7 @@
/**
* Deletes the object in the storage bucket.
+ *
* @param bucket
* @param objectKey
* @return
@@ -37,6 +41,7 @@
/**
* Get the contents of an object.
+ *
* @param bucket
* @param objectKey
* @return
@@ -45,6 +50,8 @@
void putObject(String bucket, String objectKey, InputStream input);
+ void putObject(String bucket, String objectKey, InputStream input, String contentType);
+
void createClient();
}
diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/AliyunOssServiceImpl.java b/ard-oss/src/main/java/com/ard/oss/service/impl/AliyunOssServiceImpl.java
index 2e5a941..a2151b7 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/AliyunOssServiceImpl.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/AliyunOssServiceImpl.java
@@ -99,6 +99,11 @@
}
@Override
+ public void putObject(String bucket, String objectKey, InputStream input, String contentType) {
+
+ }
+
+ @Override
public void createClient() {
if (Objects.nonNull(this.ossClient)) {
return;
diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/AmazonS3ServiceImpl.java b/ard-oss/src/main/java/com/ard/oss/service/impl/AmazonS3ServiceImpl.java
index 0ba005d..befaa74 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/AmazonS3ServiceImpl.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/AmazonS3ServiceImpl.java
@@ -84,6 +84,12 @@
PutObjectResult objectResult = client.putObject(new PutObjectRequest(bucket, objectKey, input, new ObjectMetadata()));
log.info("Upload File: {}", objectResult.toString());
}
+
+ @Override
+ public void putObject(String bucket, String objectKey, InputStream input, String contentType) {
+
+ }
+
@Override
public void createClient() {
if (Objects.nonNull(this.client)) {
diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
index 5097bce..38718a3 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
@@ -167,6 +167,43 @@
}
}
@Override
+ public void putObject(String bucket, String objectKey, InputStream input, String contentType) {
+ try {
+ // 检查文件是否已经存在
+ client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build());
+ throw new RuntimeException("The file already exists: " + objectKey);
+ } catch (ErrorResponseException e) {
+ // 如果文件不存在,则继续上传
+ log.info("The file does not exist, start uploading.");
+ try {
+ // 设置 partSize 为 10MB
+ long partSize = 10 * 1024 * 1024; // 每块 10MB
+ PutObjectArgs.Builder putArgsBuilder = PutObjectArgs.builder()
+ .bucket(bucket)
+ .object(objectKey)
+ .stream(input, -1, partSize);
+
+ // 如果提供了 Content-Type,则设置该类型
+ if (contentType != null && !contentType.isEmpty()) {
+ putArgsBuilder.contentType(contentType);
+ }
+
+ // 上传文件
+ ObjectWriteResponse response = client.putObject(putArgsBuilder.build());
+ log.info("Uploaded file successfully: {}, ETag: {}", objectKey, response.etag());
+
+ } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException ex) {
+ log.error("Failed to upload file: {}", objectKey, ex);
+ throw new RuntimeException("File upload failed: " + objectKey, ex);
+ }
+ } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
+ // 如果 statObject 发生错误,说明服务端错误或其他问题
+ log.error("Failed to check if file exists: {}", objectKey, e);
+ throw new RuntimeException("Failed to check if file exists: " + objectKey, e);
+ }
+ }
+
+ @Override
public void createClient() {
if (Objects.nonNull(this.client)) {
return;
diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java b/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
index 9fcfba5..cc61355 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
@@ -88,7 +88,7 @@
*/
public String upload(MultipartFile file) throws IOException {
try {
- return upload(OssConfiguration.bucket, null, file);
+ return upload(OssConfiguration.bucket, file);
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
@@ -131,10 +131,13 @@
? extractFilename(file)
: object;
+ // 通过文件扩展名获取 MIME 类型
+ String contentType = getMimeTypeByExtension(file.getOriginalFilename());
+
@Cleanup
InputStream inputStream = file.getInputStream();
// 上传文件到 Minio
- this.ossService.putObject(bucket, objectName, inputStream);
+ this.ossService.putObject(bucket, objectName, inputStream, contentType);
// ✅ 拼接永久 URL(不带 presigned 过期参数)
String baseUrl = OssConfiguration.endpoint;
@@ -165,4 +168,36 @@
}
}
+ // 获取文件 MIME 类型
+ public String getMimeTypeByExtension(String fileName) {
+ // 获取文件扩展名
+ String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
+
+ // 根据文件扩展名返回 MIME 类型
+ switch (extension) {
+ case "jpg":
+ case "jpeg":
+ return "image/jpeg";
+ case "png":
+ return "image/png";
+ case "gif":
+ return "image/gif";
+ case "pdf":
+ return "application/pdf";
+ case "txt":
+ return "text/plain";
+ case "html":
+ return "text/html";
+ case "json":
+ return "application/json";
+ case "csv":
+ return "text/csv";
+ case "zip":
+ return "application/zip";
+ // 添加更多的扩展名映射
+ default:
+ return "application/octet-stream"; // 默认 MIME 类型
+ }
+ }
+
}
--
Gitblit v1.9.3