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
Adding Mimetypes to MimetypesFileTypeMap
提问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 MimetypesFileTypeMap
JavaDocs.
甚至陌生人点。前几天,一位路人 (Andrew T.) 正在对 MIME 类型进行一些调查,并在MimetypesFileTypeMap
JavaDocs 中发现了此文档。
MIME types file search order:
MIME 类型文件搜索顺序:
The MimetypesFileTypeMap
looks 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:
MimetypesFileTypeMap
MIME 类型文件条目在用户系统中不同位置的外观。当请求在 中搜索 MIME 类型时MimetypesFileTypeMap
,它会按以下顺序搜索 MIME 类型文件:
- Programmatically added entries to the
MimetypesFileTypeMap
instance. - The file
.mime.types
in the user's home directory. - The file
<java.home>/lib/mime.types
. - The file or resources named
META-INF/mime.types
. - The file or resource named
META-INF/mimetypes.default
(usually found only in theactivation.jar
file). (A)
- 以编程方式向
MimetypesFileTypeMap
实例添加条目。 - 该文件
.mime.types
在用户的主目录。 - 该文件
<java.home>/lib/mime.types
。 - 名为 的文件或资源
META-INF/mime.types
。 - 命名的文件或资源
META-INF/mimetypes.default
(通常只在activation.jar
文件中找到)。(一个)
(A) As demonstrated by the code posted by jwrobel, the Jar actually seems to be the resources.jar
on 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.jar
on 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 false
result 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”,如上所示。