Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Mimetypes to 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.

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.


MIME types file search order:

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:

  1. Programmatically added entries to the MimetypesFileTypeMap instance.
  2. The file .mime.types in 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.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?

like image 827
Justin Wrobel Avatar asked Jun 10 '11 14:06

Justin Wrobel


1 Answers

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
like image 117
Justin Wrobel Avatar answered Oct 14 '22 19:10

Justin Wrobel