Mime Type(Content-Type)
MIME Type介绍 Content-Type 各种定义
多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions)是一个互联网标准,它扩展了电子邮件标准,使其能够支持非ASCII字符、二进制格式附件等多种格式的邮件消息。在万维网中使用的HTTP协议中也使用了MIME的框架,标准被扩展为互联网媒体类型。通常来说,浏览器通过MIME Type区分不同的媒体资源。目前版本:1.0。
MIME Type是经由IETF组织协商,以RFC形式作为建议标准发布在网上,通常只有一些在互联网上获得广泛应用的格式才会获得一个 MIME Type,如果是某个客户端自己定义的格式,一般只能以application/x- 开头。处理本地的文件,在没有人告诉浏览器某个文件的MIME Type的情况下,浏览器也会做一些默认的处理,这可能和在操作系统中给文件配置的MIME Type有关。
在HTTP中,MIME Type类型被定义在Content-Type header中,每个MIME类型由3部分组成:Content-Type: [type]/[subtype],前者是数据大类别,后者定义具体的种类。
通过文件扩展名获取ContentType
import lombok.Getter; import org.apache.commons.lang3.StringUtils; /** * 通过文件扩展名获取ContentType */ @Getter public enum MimeTypeEnum { AAC("acc", "AAC音频", "audio/aac"), ABW("abw", "AbiWord文件", "application/x-abiword"), ARC("arc", "存档文件", "application/x-freearc"), AVI("avi", "音频视频交错格式", "video/x-msvideo"), AZW("azw", "亚马逊Kindle电子书格式", "application/vnd.amazon.ebook"), BIN("bin", "任何类型的二进制数据", "application/octet-stream"), BMP("bmp", "Windows OS / 2位图图形", "image/bmp"), BZ("bz", "BZip存档", "application/x-bzip"), BZ2("bz2", "BZip2存档", "application/x-bzip2"), CSH("csh", "C-Shell脚本", "application/x-csh"), css("css", "级联样式表(CSS)", "text/css"), CSV("csv", "逗号分隔值(CSV)", "text/csv"), DOC("doc", "微软Word文件", "application/msword"), DOCX("docx", "Microsoft Word(OpenXML)", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), EOT("eot", "MS Embedded OpenType字体", "application/vnd.ms-fontobject"), EPUB("epub", "电子出版物(EPUB)", "application/epub+zip"), GZ("gz", "GZip压缩档案", "application/gzip"), GIF("gif", "图形交换格式(GIF)", "image/gif"), HTM("htm", "超文本标记语言(html)", "text/html"), HTML("html", "超文本标记语言(HTML)", "text/html"), ICO("ico", "图标格式", "image/vnd.microsoft.icon"), ICS("ics", "iCalendar格式", "text/calendar"), JAR("jar", "java存档", "application/java-archive"), JPEG("jpeg", "JPEG图像", "image/jpeg"), JPG("jpg", "JPEG图像", "image/jpeg"), js("js", "JavaScript", "text/javascript"), JSON("json", "JSON格式", "application/json"), JSONLD("jsonld", "JSON-LD格式", "application/ld+json"), MID("mid", "乐器数字接口(MIDI)", "audio/midi"), MIDI("midi", "乐器数字接口(MIDI)", "audio/midi"), MJS("mjs", "JavaScript模块", "text/javascript"), MP3("mp3", "MP3音频", "audio/mpeg"), MPEG("mpeg", "MPEG视频", "video/mpeg"), MPKG("mpkg", "苹果安装程序包", "application/vnd.apple.installer+xml"), ODP("odp", "OpenDocument演示文稿文档", "application/vnd.oasis.opendocument.presentation"), ODS("ods", "OpenDocument电子表格文档", "application/vnd.oasis.opendocument.spreadsheet"), ODT("odt", "OpenDocument文字文件", "application/vnd.oasis.opendocument.text"), OGA("oga", "OGG音讯", "audio/ogg"), OGV("ogv", "OGG视频", "video/ogg"), OGX("ogx", "OGG", "application/ogg"), OPUS("opus", "OPUS音频", "audio/opus"), OTF("otf", "otf字体", "font/otf"), PNG("png", "便携式网络图形", "image/png"), PDF("pdf", "Adobe 可移植文档格式(PDF)", "application/pdf"), PHP("php", "php", "application/x-httpd-php"), PPT("ppt", "Microsoft PowerPoint", "application/vnd.ms-powerpoint"), PPTX("pptx", "Microsoft PowerPoint(OpenXML)", "application/vnd.openxmlformats-officedocument.presentationml.presentation"), RAR("rar", "RAR档案", "application/vnd.rar"), RTF("rtf", "富文本格式", "application/rtf"), SH("sh", "Bourne Shell脚本", "application/x-sh"), SVG("svg", "可缩放矢量图形(SVG)", "image/svg+xml"), SWF("swf", "小型Web格式(SWF)或Adobe Flash文档", "application/x-shockwave-flash"), TAR("tar", "磁带存档(TAR)", "application/x-tar"), TIF("tif", "标记图像文件格式(TIFF)", "image/tiff"), TIFF("tiff", "标记图像文件格式(TIFF)", "image/tiff"), TS("ts", "MPEG传输流", "video/mp2t"), TTF("ttf", "ttf字体", "font/ttf"), TXT("txt", "文本(通常为ASCII或ISO 8859- n", "text/plain"), VSD("vsd", "微软Visio", "application/vnd.visio"), WAV("wav", "波形音频格式", "audio/wav"), WEBA("weba", "WEBM音频", "audio/webm"), WEBM("webm", "WEBM视频", "video/webm"), WEBP("webp", "WEBP图像", "image/webp"), WOFF("woff", "Web开放字体格式(WOFF)", "font/woff"), WOFF2("woff2", "Web开放字体格式(WOFF)", "font/woff2"), XHTML("xhtml", "XHTML", "application/xhtml+xml"), XLS("xls", "微软excel", "application/vnd.ms-excel"), XLSX("xlsx", "微软Excel(OpenXML)", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), XML("xml", "XML", "application/xml"), XUL("xul", "XUL", "application/vnd.mozilla.xul+xml"), ZIP("zip", "ZIP", "application/zip"), MIME_3GP("3gp", "3GPP audio/video container", "video/3gpp"), MIME_3GP_WITHOUT_VIDEO("3gp", "3GPP audio/video container doesn't contain video", "audio/3gpp2"), MIME_3G2("3g2", "3GPP2 audio/video container", "video/3gpp2"), MIME_3G2_WITHOUT_VIDEO("3g2", "3GPP2 audio/video container doesn't contain video", "audio/3gpp2"), MIME_7Z("7z", "7-zip存档", "application/x-7z-compressed"); //扩展名 private final String extension; //说明 private final String explain; //contentType/mime类型 private final String mimeType; /** * @param extension 上传的文件扩展名 * @param explain 类型说明 * @param mimeType Mime对应的类型 */ MimeTypeEnum(String extension, String explain, String mimeType) { this.extension = extension; this.explain = explain; this.mimeType = mimeType; } /** * 通过扩展名获取枚举类型 * * @param extension 扩展名 * @return 枚举类 */ public static MimeTypeEnum getByExtension(String extension) { if (StringUtils.isEmpty(extension)) { return null; } for (MimeTypeEnum typesEnum : MimeTypeEnum.values()) { if (extension.equals(typesEnum.getExtension())) { return typesEnum; } } return null; } /** * Content-Type常用对照 * 根据后缀获取Mime * * @param fileType 扩展名 * @return mime类型 */ public static String getContentType(String fileType) { MimeTypeEnum mimeTypeEnum = MimeTypeEnum.getByExtension(fileType); if (mimeTypeEnum != null) { return mimeTypeEnum.getMimeType(); } return "application/octet-stream"; } }
根据文件获取MimeType的几种方式
<!-- hutool-all --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.6.5</version> </dependency> <!-- commons.lang3--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.79</version> </dependency> <dependency> <groupId>net.sf.jmimemagic</groupId> <artifactId>jmimemagic</artifactId> <version>0.1.4</version> </dependency>
import cn.hutool.core.io.FileUtil; import net.sf.jmimemagic.*; import javax.activation.MimetypesFileTypeMap; import java.io.File; import java.io.IOException; import java.net.FileNameMap; import java.net.URLConnection; import java.nio.file.Files; public class MimeTypeDemo { public static void main(String[] args) { File file = FileUtil.file("d:/001.jpg"); // 第一种方式 String mimeType1 = ""; try { Magic parser = new Magic(); MagicMatch match = parser.getMagicMatch(file, false); mimeType1 = match.getMimeType(); } catch (MagicParseException e) { e.printStackTrace(); } catch (MagicMatchNotFoundException e) { e.printStackTrace(); } catch (MagicException e) { e.printStackTrace(); } System.out.println("第一种方式: " + mimeType1); // 第二种方式 String mimeType2 = new MimetypesFileTypeMap().getContentType(file); System.out.println("第二种方式: " + mimeType2); // 第三种方式 String mimeType3 = ""; try { mimeType3 = Files.probeContentType(file.toPath()); } catch (IOException e) { e.printStackTrace(); } System.out.println("第三种方式: " + mimeType3); // 第四种方式 FileNameMap fileNameMap = URLConnection.getFileNameMap(); String mimeType4 = fileNameMap.getContentTypeFor(file.getParent()); System.out.println("第四种方式: " + mimeType4); // 通过文件扩展名获取ContentType MimeTypeEnum mimeTypeEnum = MimeTypeEnum.getByExtension("jpg"); System.out.println(mimeTypeEnum.getExplain()+ " , "+mimeTypeEnum.getMimeType()+ " , "+mimeTypeEnum.getExtension()); } }