Java Content Type Get getContentType(String name)

Here you can find the source of getContentType(String name)

Description

Gets the content type for the specified filename.

License

Open Source License

Parameter

Parameter Description
name the name of a file

Return

a MIME type, or application/octet-stream if one can't be found

Declaration

public static String getContentType(String name) 

Method Source Code


//package com.java2s;

import java.net.FileNameMap;
import java.net.URLConnection;

import java.util.Properties;

public class Main {
    /** the local content type map */
    private static Properties localFileNameMap;

    /**/* w  ww.j av a 2s  . c  o m*/
     * Gets the content type for the specified filename.
     *
     * @param name    the name of a file
     * @return  a MIME type, or application/octet-stream if one can't be found
     */
    public static String getContentType(String name) {
        String contentType;
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        contentType = fileNameMap.getContentTypeFor(name);

        if (contentType == null) {
            int index = name.lastIndexOf(".");
            if (index > -1) {
                contentType = localFileNameMap.getProperty(name.substring(index));
            }
        }

        return contentType;
    }
}

Related

  1. getContentType(final String filename)
  2. getContentType(String boundary)
  3. getContentType(String file)
  4. getContentType(String fileName)
  5. getContentType(String filename)