SpringBoot Java获取文件的Mime类型 ContentType 开箱即用

常见 MIME 类型列表
有几十种MIME类型,网上写了很多种方法,我觉得有缺陷,SpringBoot有MediaType对应,肯定有对应的方法去获取文件的Mime类型 ,根据Spring代码的写了一个方法。

java 复制代码
    /**
     * 根据文件名,设置HttpServletResponse的ContentType
     *
     * @param fileName 文件名
     * @return 结果
     * @author weimingzhong
     * @date 2024/4/3 9:50
     */
    public static String getContentType(String fileName) {
        Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(fileName);
        return String.valueOf(mediaType.orElse(MediaType.APPLICATION_OCTET_STREAM));
    }
相关推荐
文丑颜不良啊3 小时前
LeetCode2981~3000题目解析
java·leetcode
nice_lcj5205 小时前
【从零理解 Spring AI · 13】RAG 要解决什么:把“闭卷考试“变成“开卷“
java·spring·检索增强·rag·spring ai·ai应用开发
Csvn5 小时前
Nginx 静态站点与 location 匹配:优先级、alias、缓存头与 gzip
后端·nginx
小满zs6 小时前
Go语言第二十章(初探Gin)
后端·go
学长毕设6 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
小满zs6 小时前
Go语言第十九章(http标准库)
后端·go
这个昵称不容易6 小时前
SpringBoot 医疗病历交互系统的设计与实现:192 个 Java 文件、256 个接口、22 张表,附真实运行截图
java·springboot·毕设
驭渊的小故事7 小时前
订单服务如何调用商品服务?一次 RestTemplate 跨服务调用入门
java·开发语言·php
考虑考虑8 小时前
nginx拦截url
运维·后端·nginx
GISer_Jing8 小时前
GitHub热点I
后端·ai·langchain