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.
Am I missing something?
It is a Spring/Maven Web project. Any help would be appreciated!
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.
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
instance..mime.types
in the user's home directory.<java.home>/lib/mime.types
.META-INF/mime.types
.META-INF/mimetypes.default
(usually found only in the activation.jar
file). (A)
(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"));
}
}
..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. 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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With