将Mimetypes添加到MimetypesFileTypeMap [英] Adding Mimetypes to MimetypesFileTypeMap
问题描述
我在将Mimetypes添加到MimetypesFileTypeMap时遇到问题。我尝试添加一个META-INF / mime.types文件,就像文档说。但它似乎没有被MimetypesFileTypeMap读取。
I'm having trouble adding Mimetypes to MimetypesFileTypeMap. I've tried adding a META-INF/mime.types file just like the Documentation says to. but it doesn't seem like it is getting read by MimetypesFileTypeMap.
我错过了什么?
这是一个Spring / Maven Web项目。任何帮助将不胜感激!
It is a Spring/Maven Web project. Any help would be appreciated!
更奇怪的一点。一位路人(Andrew T.)前几天正在对MIME类型进行一些调查。在 MimetypesFileTypeMap
JavaDocs中发现了这个文档。
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.
MimetypesFileTypeMap
在用户系统的各个位置查找MIME类型文件条目。当请求在 MimetypesFileTypeMap
中搜索MIME类型时,它按以下顺序搜索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 .types
在用户的主目录中。 - 文件
< java.home> /lib/mime.types
。 - 名为
META-INF / mime.types
的文件或资源。 - 名为
META-INF / mimetypes.default
的文件或资源(通常仅在activation.jar
文件中找到)。 (A)
- 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)
(A)正如由jwrobel发布的代码,Jar实际上似乎是 resources.jar
至少有两个系统(使用JDK)。
(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).
所以给出了这个来源。 。
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"));
}
}
..我们可以排除'1' - 以编程方式添加。我们也可以忘记4& 5如果我们从没有 META-INF
的目录中的命令行运行它,没有 activation.jar
类路径。只留下选项2& 3。
..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.
然而这个来源的输出是..
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。两个文件上的 false
结果可能是为文件字符串显示的MIME映射的来源,建议最后两个信息源。不存在那么来自哪里的信息呢?
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?
推荐答案
Spring提供了包装类,其中包含更新的MIME类型列表。您使用它的方式与使用MimetypesFileTypeMap的方式非常相似。
Spring provides a wrapper class which comes packed with a more updated MIME type list. You use it pretty much the same way you'd use MimetypesFileTypeMap.
import org.springframework.mail.javamail.ConfigurableMimeFileTypeMap;
...
ConfigurableMimeFileTypeMap mimeMap = new ConfigurableMimeFileTypeMap();
String contentType = mimeMap.getContentType(uploadedName);//defaults to application/octet-stream
这篇关于将Mimetypes添加到MimetypesFileTypeMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!