Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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() + "  - " +f);

        f = new File(System.getProperty("user.home"), ".mime.types");
        System.out.println(f.exists() + "  - " +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 FilesJavajdk1.6.0jrelibmime.types
false    - C:UsersAndrew.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
100 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...