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));
    }
相关推荐
weisian1518 分钟前
Java并发编程--48-美团Leaf与百度UidGenerator:分布式ID生成器的工业级实践
java·leaf号段模式·leaf雪花模式·uidgenerator
胖纳特10 分钟前
Nextcloud 文件预览困局与破局:集成 BaseMetas Fileview 实现全格式在线预览
前端·后端
lczllx10 分钟前
MIT 6.824-lab3A(实现思路)
后端
一个心烑10 分钟前
Layui结合springboot读取返回值,前端展示简单示例
前端·spring boot·layui
派星10 分钟前
AOP实践:公共字段自动填充
后端
郝开12 分钟前
Spring Cloud Gateway 3.5.14 使用手册
java·数据库·spring boot·gateway
用户05101225729613 分钟前
从 malloc 到 DMA-BUF:嵌入式边缘设备上 mmap 的缓存一致性真相
后端
lczllx14 分钟前
MIT 6.824 MapReduce(实现思路)
后端
lczllx14 分钟前
C语言实现三子棋(详细讲解+源码)
后端
techdashen14 分钟前
Cloudflare 用 Rust 实现 QUIC 协议:quiche 是怎么设计的
开发语言·后端·rust