Robert Hume Robert Hume - 14 days ago 8
Java Question

Why does MimetypesFileTypeMap always return "application/octet-stream" for PNG file?

I'm trying to use

javax.activation.MimetypesFileTypeMap
to get the content type.

For the string "image.png" it always returns "application/octect-stream" ... shouldn't it return something like "image/png" ?

javax.activation.MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType("image.png");

Answer

See the Javadocs of javax.activation.MimetypesFileTypeMap. The method looks up a file called .mime.types in a certain order:

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).

If no file is found, getContentType method returns application/octet-stream:

Return the MIME type based on the specified file name. The MIME type entries are searched as described above under MIME types file search order. If no entry is found, the type "application/octet-stream" is returned.