Spring Boot中如何根据文件扩展名获取contentType呢?
下文笔者讲述SpringBoot中根据扩展名返回contentType的方法分享,如下所示
文件扩展名返回contentType的方法
借助MediaTypeFactory.getMediaType方法 即可获取一个文件名对应的contentType信息 注意事项: Spring中所有的mime-type信息存储在 spring-web-5.x.x.jar!\org\springframework\http\mime.types中例:文件名返回contentType
String filename = "java265测试图片.png"; Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(filename); mediaType.ifPresent(type -> response.setContentType(type.toString()));
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。