woody 6 mesi fa
parent
commit
c635bbf02f

+ 20 - 0
framework-base/src/main/java/com/chelvc/framework/base/util/HttpUtils.java

@@ -24,12 +24,14 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import com.chelvc.framework.base.interceptor.BufferedRequestWrapper;
+import com.chelvc.framework.common.model.Media;
 import com.chelvc.framework.common.util.FileUtils;
 import com.chelvc.framework.common.util.HostUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.google.common.collect.Maps;
 import lombok.NonNull;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.springframework.http.MediaType;
 import org.springframework.web.multipart.MultipartFile;
@@ -40,6 +42,7 @@ import org.springframework.web.multipart.MultipartFile;
  * @author Woody
  * @date 2024/1/30
  */
+@Slf4j
 public final class HttpUtils {
     /**
      * 资源分隔符匹配模式
@@ -419,6 +422,23 @@ public final class HttpUtils {
         return matcher.find() ? Charset.forName(matcher.group(1)) : StandardCharsets.UTF_8;
     }
 
+    /**
+     * 获取文件媒体类型
+     *
+     * @param file 文件对象
+     * @return 媒体类型
+     */
+    public static Media getMedia(@NonNull MultipartFile file) {
+        MediaType type = null;
+        try {
+            type = StringUtils.ifEmpty(file.getContentType(), MediaType::parseMediaType);
+        } catch (Exception e) {
+            log.warn("Media type parse failed: {}", e.getMessage());
+        }
+        String value = StringUtils.ifEmpty(ObjectUtils.ifNull(type, MediaType::getType), String::toUpperCase);
+        return ObjectUtils.ifNull(StringUtils.ifEmpty(value, Media::parse), Media.NORMAL);
+    }
+
     /**
      * 获取文件后缀名
      *