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

会有一股神秘感。 提交于 2020-01-24 02:27:26

问题


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");

回答1:


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.




回答2:


I improved upon MimetypesFileTypeMap (the content type probing is too slow for web services):

package com.github.jjYBdx4IL.utils.text;

import java.io.IOException;
import java.io.InputStream;

import javax.activation.MimetypesFileTypeMap;

//CHECKSTYLE:OFF
public class MimeType {

    public static MimetypesFileTypeMap MAP = createMap();

    /**
     * 
     * @return a vastly improved mimetype map
     */
    public static MimetypesFileTypeMap createMap() {
        try (InputStream is = MimeType.class.getResourceAsStream("mimetypes.txt")) {
            return new MimetypesFileTypeMap(is);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static String get(String fileName) {
        return get(fileName, null);
    }

    public static String get(String fileName, String charset) {
        String mimeType = MAP.getContentType(fileName.toLowerCase());
        if (charset != null && (mimeType.startsWith("text/") || mimeType.contains("javascript"))) {
            mimeType += ";charset=" + charset.toLowerCase();
        }
        return mimeType;
    }


}

https://github.com/jjYBdx4IL/misc/blob/master/text-utils/src/main/java/com/github/jjYBdx4IL/utils/text/MimeType.java

application/javascript js
application/msword doc docx docm
application/pdf pdf
application/postscript ai eps ps
application/rss+xml rss
application/rtf rtf
application/vnd.ms-excel xls xlsx xlsm XLS
application/vnd.ms-powerpoint ppt pps pot pptx pptm
application/vnd.oasis.database odb
application/vnd.oasis.opendocument.text odt
application/vnd.oasis.presentation odp
application/vnd.oasis.spreadsheet ods
application/vnd.oasis.text odt
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/x-awk awk
application/x-blender blend
application/x-cd-image iso
application/x-compress zip gz tar rar
application/x-deb deb
application/x-font-otf otf OTF
application/x-font-ttf ttf TTF
application/x-java-applet class
application/x-java-archive jar
application/xml xml
application/x-ms-dos-executable exe msi
application/x-perl pl
application/x-php php
application/x-rpm rpm
application/x-sharedlib o
application/x-shellscript sh
application/x-tar tar
application/x-texinfo texinfo texi
application/x-tex tex
application/x-trash autosave
application/x-troff t tr roff
application/x-vnd.oasis.opendocument.spreadsheet ods
application/zip zip
audio/ac3 ac3
audio/basic au
audio/midi midi mid
audio/mpeg mp3 mpeg3
audio/x-aifc aifc
audio/x-aiff aif aiff
audio/x-generic wav wma mp3 ogg
audio/x-mpeg mpeg mpg
audio/x-wav wav
image/gif gif GIF
image/ief ief
image/jpeg jpeg jpg jpe JPG
image/png png PNG
image/svg+xml svg svgz
image/tiff tiff tif
image/x-eps eps
image/x-generic bmp jpg jpeg png tif tiff xpm wmf emf
image/x-xwindowdump xwd
text/css css
text/csv csv
text/html html htm HTML HTM
text/plain txt text TXT TEXT
text/richtext rtx
text/rtf rtf
text/tab-separated-values tsv tab
text/x-bibtex bib
text/x-c++hdr h
text/x-csrc c
text/x-c++src cpp c++
text/x-java java
text/x-log log
text/xml xml XML osm
text/x-pascal pas
text/x-po po pot
text/x-python py
text/x-sql sql
text/x-tcl tcl
text/x-tex tex
video/mpeg mpeg mpg mpe mpv vbs mpegv
video/msvideo avi
video/quicktime qt mov moov
video/x-generic wmv mpeg mp4 ogv swf mov dvd osp
video/x-msvideo avi

https://github.com/jjYBdx4IL/misc/blob/master/text-utils/src/main/resources/com/github/jjYBdx4IL/utils/text/mimetypes.txt

I just threw together all I could quickly find out there via Google, so I cannot say whether all of these types are actually valid.




回答3:


For getting contentType you can use

public String getFileContentType(String fileName) {
    String fileType = "Undetermined";
    final File file = new File(fileName);
    try
    {
        fileType = Files.probeContentType(file.toPath());
    }
    catch (IOException ioException)
    {
        System.out.println(
                "ERROR: Unable to determine file type for " + fileName
                        + " due to exception " + ioException);
    }
    return fileType;
}


来源:https://stackoverflow.com/questions/34165873/why-does-mimetypesfiletypemap-always-return-application-octet-stream-for-png-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!