java 将 Mimetypes 添加到 MimetypesFileTypeMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6308142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:18:41  来源:igfitidea点击:

Adding Mimetypes to MimetypesFileTypeMap

javamime-types

提问by Justin Wrobel

I'm having trouble adding Mimetypes to MimetypesFileTypeMap. I've tried adding a META-INF/mime.types file just like the Documentationsays to. but it doesn't seem like it is getting read by MimetypesFileTypeMap.

我在将 Mimetypes 添加到 MimetypesFileTypeMap 时遇到了问题。我试过添加一个 META-INF/mime.types 文件,就像文档所说的那样。但它似乎没有被 MimetypesFileTypeMap 读取。

Am I missing something?

我错过了什么吗?

It is a Spring/Maven Web project. Any help would be appreciated!

它是一个 Spring/Maven Web 项目。任何帮助,将不胜感激!



Even stranger side point. A passerby (Andrew T.) was doing some investigation into MIME types just the other day & uncovered this documentation in the MimetypesFileTypeMapJavaDocs.

甚至陌生人点。前几天,一位路人 (Andrew T.) 正在对 MIME 类型进行一些调查,并在MimetypesFileTypeMapJavaDocs 中发现了此文档。



MIME types file search order:

MIME 类型文件搜索顺序:

The MimetypesFileTypeMaplooks in various places in the user's system for MIME types file entries. When requests are made to search for MIME types in the MimetypesFileTypeMap, it searches MIME types files in the following order:

MimetypesFileTypeMapMIME 类型文件条目在用户系统中不同位置的外观。当请求在 中搜索 MIME 类型时MimetypesFileTypeMap,它会按以下顺序搜索 MIME 类型文件:

  1. Programmatically added entries to the MimetypesFileTypeMapinstance.
  2. The file .mime.typesin the user's home directory.
  3. The file <java.home>/lib/mime.types.
  4. The file or resources named META-INF/mime.types.
  5. The file or resource named META-INF/mimetypes.default(usually found only in the activation.jarfile). (A)
  1. 以编程方式向MimetypesFileTypeMap实例添加条目。
  2. 该文件.mime.types在用户的主目录。
  3. 该文件<java.home>/lib/mime.types
  4. 名为 的文件或资源META-INF/mime.types
  5. 命名的文件或资源META-INF/mimetypes.default(通常只在activation.jar文件中找到)。(一个)

(A) As demonstrated by the code posted by jwrobel, the Jar actually seems to be the resources.jaron at least two systems (with JDKs).

(A) 正如jwrobel 发布代码所证明的,Jar 实际上似乎是resources.jar至少两个系统上的(使用 JDK)。



So given this source..

所以鉴于这个来源..

import java.io.File;
import javax.activation.MimetypesFileTypeMap;

class TestMime {
    public static void main(String[] args) {
        System.out.println(System.getProperty("java.version"));

        File f = new File(System.getProperty("java.home"), "lib");
        f = new File(f, "mime.types");
        System.out.println(f.exists() + " \t - " +f);

        f = new File(System.getProperty("user.home"), ".mime.types");
        System.out.println(f.exists() + " \t - " +f);

        MimetypesFileTypeMap mfm = new MimetypesFileTypeMap();
        System.out.println(mfm.getContentType("a.js"));
        System.out.println(mfm.getContentType("a.png"));
        System.out.println(mfm.getContentType("a.jpg"));
        System.out.println(mfm.getContentType("a.au"));
        System.out.println(mfm.getContentType("a.htm"));
    }
}

..we can rule out '1' - programmatically added. We can also forget 4 & 5 if we run it from the command line in a directory with no META-INF, no activation.jaron the class-path. Which only leaves options 2 & 3.

..我们可以排除'1' - 以编程方式添加。如果我们从命令行在类路径上没有META-INF, no的目录中运行它,我们也可以忘记 4 和 5 activation.jar。只剩下选项 2 和 3。

Yet the output of this source is..

然而这个源的输出是..

1.6.0
false    - C:\Program Files\Java\jdk1.6.0\jre\lib\mime.types
false    - C:\Users\Andrew\.mime.types
application/octet-stream
application/octet-stream
image/jpeg
audio/basic
text/html
Press any key to continue . . .

I.E. The falseresult on both files that could be the source of the MIME mappings shown for the file strings, suggest those last two source of info. don't exist. So where the heck is the information coming from?

IEfalse两个文件的结果可能是为文件字符串显示的 MIME 映射的来源,建议最后两个信息来源。不存在。那么这些信息到底是从哪里来的呢?

采纳答案by Justin Wrobel

Spring provides a wrapper classwhich comes packed with a more updated MIME type list. You use it pretty much the same way you'd use MimetypesFileTypeMap.

Spring 提供了一个包装类,其中包含更新的 MIME 类型列表。您使用它的方式与使用 MimetypesFileTypeMap 的方式几乎相同。

import org.springframework.mail.javamail.ConfigurableMimeFileTypeMap;
...
ConfigurableMimeFileTypeMap mimeMap = new ConfigurableMimeFileTypeMap();
String contentType = mimeMap.getContentType(uploadedName);//defaults to application/octet-stream

回答by Justin Wrobel

To answer Andrew Thompson, I think that MimetypesFileTypeMap is getting its default MIME Type definitions from /lib/resources.jar:/META-INF/mimetypes.default.

为了回答 Andrew Thompson,我认为 MimetypesFileTypeMap 正在从 /lib/resources.jar:/META-INF/mimetypes.default 获取其默认的 MIME 类型定义。

Running this code from the command line:

从命令行运行此代码:

ClassLoader loader = Thread.currentThread().getContextClassLoader(); 
URL resource = loader.getResource("META-INF/mimetypes.default"); 
System.out.println(resource.getPath());

Yields:

产量

file:/usr/java/jdk1.6.0_22/jre/lib/resources.jar!/META-INF/mimetypes.default

MimetypesFileTypeMapuses ClassLoader.getResource() to load the default mimetypes.

MimetypesFileTypeMap使用 ClassLoader.getResource() 加载默认的 mimetypes。

回答by Vespucci75fr

You can add a mimetype to MimetypesFileTypeMap in your Javacode

您可以在 Java 代码中向 MimetypesFileTypeMap 添加 mimetype

MimetypesFileTypeMap mimeType = new MimetypesFileTypeMap();
mimeType.addMimeTypes("application/pdf pdf");

The key info here is addMimeTypes argument must be "mime_type extension" eg: "application/pdf pdf" as shown above.

这里的关键信息是 addMimeTypes 参数必须是“mime_type extension”,例如:“application/pdf pdf”,如上所示。