Java Mime Type Get getExactMimeType(String fileName)

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

Description

This gets the MIME type of a file, based on its name.

License

Open Source License

Declaration

static public String getExactMimeType(String fileName) 

Method Source Code

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

import java.net.*;

public class Main {
    /** This gets the MIME type of a file, based on its name. If no matching MIME type is found, this method returns null. It works by calling {@link URLConnection#getFileNameMap}, and does some custom processing for known types (js, tgz, css, ico, and sit) that are not in that map. **/
    static public String getExactMimeType(String fileName) {
        if (fileName == null)
            return null;
        String mimeType = URLConnection.getFileNameMap().getContentTypeFor(fileName);
        if (mimeType == null) {
            try {
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                if ("js".equals(suffix))
                    mimeType = "text/javascript";
                else if ("tgz".equals(suffix))
                    mimeType = "application/x-tar";
                else if ("css".equals(suffix))
                    mimeType = "text/css";
                else if ("ico".equals(suffix))
                    mimeType = "image/x-icon";
                else if ("sit".equals(suffix))
                    mimeType = "application/x-stuffit";
                else if ("fdx".equals(suffix))
                    mimeType = "application/xml";
            } catch (Exception err) {
            }/*from   w ww.j  av  a  2s  . c o  m*/
        }
        return mimeType;
    }
}

Related

  1. extractMimeType(byte[] imageData)
  2. getMimeType(File file)
  3. getMimeType(String file)
  4. getMimeType(String fileName)
  5. getMIMEType(String fileName)