如何从文件扩展名中获得MIME类型?


当前回答

当然,为了将通用完整性、动态性和可移植性的优势与最小的依赖性结合起来

你将结合Samuel Neff的静态字典与特定平台的查询,例如由Bryan Denny。

例如,在Linux下,命令行工具“xdg-mime”可以完成后面的部分。

其他回答

你可以使用mimemaps类。我认为这是最简单的方法。我也给出了mimemapping的导入。因为我觉得找这些类的导入很麻烦。

import org.springframework.boot.web.server.MimeMappings;    
MimeMappings mm=new MimeMappings();
String mimetype = mm.get(fileExtension);
System.out.println(mimetype);

当然,为了将通用完整性、动态性和可移植性的优势与最小的依赖性结合起来

你将结合Samuel Neff的静态字典与特定平台的查询,例如由Bryan Denny。

例如,在Linux下,命令行工具“xdg-mime”可以完成后面的部分。

IANA媒体类型

我希望微软能把他们的行业标准统一起来!对于其他人,有兴趣的人:

离散的类型

application: https://www.iana.org/assignments/media-types/media-types.xhtml#application audio: https://www.iana.org/assignments/media-types/media-types.xhtml#audio example: https://www.iana.org/assignments/media-types/media-types.xhtml#examples font: https://www.iana.org/assignments/media-types/media-types.xhtml#font image: https://www.iana.org/assignments/media-types/media-types.xhtml#image model: https://www.iana.org/assignments/media-types/media-types.xhtml#model text: https://www.iana.org/assignments/media-types/media-types.xhtml#text video: https://www.iana.org/assignments/media-types/media-types.xhtml#video

多部分类型

信息:https://www.iana.org/assignments/media-types/media-types.xhtml消息 多部分:https://www.iana.org/assignments/media-types/media-types.xhtml #多部分

我想推荐一个阅读:MIME类型(IANA媒体类型)的Mozilla页面感兴趣的人!它的信息量很大!

在代码方面,上面的每个链接都有一个.csv文件下载:https://www.iana.org/assignments/media-types/application.csv

正如这里已经指出的,可以使用Dictionary或ConcurrentDictionary下载并使用Key - Value对填充Dictionary。

ASP。NET或其他

在ASP中,选项发生了一些变化。NET Core,在这里(学分):

新FileExtensionContentTypeProvider()。TryGetContentType(文件名,输出内容类型);(仅vNext) 从未测试过,但看起来您可以通过公开的Mappings属性正式展开mime类型列表。 使用MimeTypes NuGet包 从.NET Framework的引用源中复制mimemaps文件

对于。net Framework >= 4.5:

使用System.Web.MimeMapping.GetMimeMapping方法,这是.NET Framework 4.5中BCL的一部分:

string mimeType = MimeMapping.GetMimeMapping(fileName);

如果你需要添加自定义映射,你可以使用反射来向BCL MimeMapping类添加映射,它使用一个自定义字典来公开这个方法,所以你应该调用下面的方法来添加映射(从未测试过,但应该问题。工作)。

无论如何,当使用反射来添加MIME类型时,要注意,由于您正在访问私有字段,因此它的名称可能会更改甚至完全被删除,因此您应该格外谨慎,添加双重检查并为每一步提供故障安全操作。

MimeMapping._mappingDictionary.AddMapping(string fileExtension, string mimeType)

为了使这篇文章更全面,对于。net核心开发人员有FileExtensionContentTypeProvider类,它涵盖了官方的MIME内容类型。

它在幕后工作——根据文件扩展名在Http响应头中设置ContentType。

如果您需要特殊的MIME类型,请参阅自定义MIME类型的示例:

public void Configure(IApplicationBuilder app)
{
    // Set up custom content types -associating file extension to MIME type
    var provider = new FileExtensionContentTypeProvider();
    // Add new mappings
    provider.Mappings[".myapp"] = "application/x-msdownload";
    provider.Mappings[".htm3"] = "text/html";
    provider.Mappings[".image"] = "image/png";
    // Replace an existing mapping
    provider.Mappings[".rtf"] = "application/x-msdownload";
    // Remove MP4 videos.
    provider.Mappings.Remove(".mp4");

    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "images")),
        RequestPath = new PathString("/MyImages"),
        ContentTypeProvider = provider
    });

    app.UseDirectoryBrowser(new DirectoryBrowserOptions()
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "images")),
        RequestPath = new PathString("/MyImages")
    });
}