Java Content Type Get getContentType(final File file)

Here you can find the source of getContentType(final File file)

Description

Get a file mime type based on its filename.

License

Open Source License

Parameter

Parameter Description
file a parameter

Return

String mime type.

Declaration

public static String getContentType(final File file) 

Method Source Code


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

import java.io.File;

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

public class Main {
    /** For looking up file mime types. */
    private static final FileNameMap MIMETYPES = URLConnection.getFileNameMap();

    /**/*w  w  w.  j a v a2 s.  c o m*/
     * Get a file mime type based on its filename.
     * 
     * Calls getContentType(file.getName()).
     * 
     * @param file
     * @return String mime type.
     */
    public static String getContentType(final File file) {
        return getContentType(file.getName());
    }

    /**
     * Get a file mime type based on its file path extension.
     * 
     * Uses URLConnection.getFileNameMap().
     * 
     * @param filename
     *            file path.
     * @return String mime type.
     */
    public static String getContentType(final String filename) {
        return MIMETYPES.getContentTypeFor(filename);
    }
}

Related

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