liusuyi
2025-10-30 9f911c1461ba33c1a561046eb1ac8e1c56fc9b9d
ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
@@ -10,6 +10,7 @@
import com.ard.oss.model.enums.OssTypeEnum;
import com.ard.oss.service.IOssService;
import io.minio.PutObjectArgs;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@@ -87,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);
        }
@@ -130,8 +131,13 @@
                    ? extractFilename(file)
                    : object;
            // 通过文件扩展名获取 MIME 类型
            String contentType = getMimeTypeByExtension(file.getOriginalFilename());
            @Cleanup
            InputStream inputStream = file.getInputStream();
            // 上传文件到 Minio
            this.ossService.putObject(bucket, objectName, file.getInputStream());
            this.ossService.putObject(bucket, objectName, inputStream, contentType);
            // ✅ 拼接永久 URL(不带 presigned 过期参数)
            String baseUrl = OssConfiguration.endpoint;
@@ -162,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 类型
        }
    }
}