Browse Source

MinIO上传显示格式修改

gao.qiang 2 years ago
parent
commit
cab31d639d

+ 1 - 6
base-common/src/main/java/com/ozs/common/utils/MinioUtils.java

@@ -53,12 +53,6 @@ public class MinioUtils {
      * @throws Exception
      */
     public void minIoClientUpload(InputStream imgInputStream, String objectName) throws Exception {
-        //创建头部信息
-        Map<String, String> headers = new HashMap<>(10);
-        //添加自定义内容类型
-        headers.put("Content-Type", "application/octet-stream");
-        //添加存储类
-        headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
         //添加自定义/用户元数据
         Map<String, String> userMetadata = new HashMap<>(10);
         userMetadata.put("My-Project", "Project One");
@@ -67,6 +61,7 @@ public class MinioUtils {
                 PutObjectArgs.builder()
                         .bucket(bucketName)
                         .object(objectName)
+                        .contentType(ViewContentType.getContentType(objectName))
                         .stream(imgInputStream, imgInputStream.available(), -1)
                         .userMetadata(userMetadata)
                         .build());

+ 52 - 0
base-common/src/main/java/com/ozs/common/utils/ViewContentType.java

@@ -0,0 +1,52 @@
+package com.ozs.common.utils;
+
+/**
+ * @author Administrator
+ */
+
+public enum ViewContentType {
+    DEFAULT("default","application/octet-stream"),
+    JPG("jpg", "image/jpeg"),
+    TIFF("tiff", "image/tiff"),
+    GIF("gif", "image/gif"),
+    JFIF("jfif", "image/jpeg"),
+    PNG("png", "image/png"),
+    TIF("tif", "image/tiff"),
+    ICO("ico", "image/x-icon"),
+    JPEG("jpeg", "image/jpeg"),
+    WBMP("wbmp", "image/vnd.wap.wbmp"),
+    FAX("fax", "image/fax"),
+    NET("net", "image/pnetvue"),
+    JPE("jpe", "image/jpeg"),
+    RP("rp", "image/vnd.rn-realpix");
+
+    private String prefix;
+
+    private String type;
+
+    public static String getContentType(String prefix){
+        if(StringUtils.isEmpty(prefix)){
+            return DEFAULT.getType();
+        }
+        prefix = prefix.substring(prefix.lastIndexOf(".") + 1);
+        for (ViewContentType value : ViewContentType.values()) {
+            if(prefix.equalsIgnoreCase(value.getPrefix())){
+                return value.getType();
+            }
+        }
+        return DEFAULT.getType();
+    }
+
+    ViewContentType(String prefix, String type) {
+        this.prefix = prefix;
+        this.type = type;
+    }
+
+    public String getPrefix() {
+        return prefix;
+    }
+
+    public String getType() {
+        return type;
+    }
+}