Java Mime Type Get getMimeType(String fileName)

Here you can find the source of getMimeType(String fileName)

Description

Get the mime type for the specified filename

License

Open Source License

Parameter

Parameter Description
fileName The file name

Return

The mime type from our map, java's map or the standard mime type

Declaration

public static String getMimeType(String fileName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.HashMap;

public class Main {
    /**//  w  w w . j  ava  2  s .co  m
     * The mime types parsed from the configuration file
     */
    private static final HashMap<String, String> mimeTypes = new HashMap<String, String>();
    /**
     * The default java type map
     */
    private static final FileNameMap fileNameMap = URLConnection.getFileNameMap();

    /**
     * Get the mime type for the specified filename
     * 
     * @param fileName
     *            The file name
     * @return The mime type from our map, java's map or the standard mime type
     */
    public static String getMimeType(String fileName) {
        if (fileName.contains(".")) {
            // Try our list, it is the most up to date
            String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
            if (mimeTypes.containsKey(extension)) {
                return mimeTypes.get(extension);
            }
        }
        // Try the java default map
        String defaultMime = fileNameMap.getContentTypeFor(fileName);
        if (defaultMime != null) {
            return defaultMime;
        }
        return "application/octet-steam";
    }
}

Related

  1. extractMimeType(byte[] imageData)
  2. getExactMimeType(String fileName)
  3. getMimeType(File file)
  4. getMimeType(String file)
  5. getMIMEType(String fileName)
  6. getMimeType(String fileName)
  7. getMimeType(String fileName)
  8. getMimeTypeByFileName(String fileName)