Java Content Type Get getContentType(final String filename)

Here you can find the source of getContentType(final String filename)

Description

Return the contentType for a given filename if it is defined inside the JVM

License

Apache License

Parameter

Parameter Description
filename a parameter

Return

the content type

Declaration

public static String getContentType(final String filename) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;

public class Main {
    /**/*from ww  w . j av a 2s  .  c o  m*/
     * Return the contentType for a given filename if it is defined inside the JVM
     * 
     * @param filename
     * @return the content type
     */
    public static String getContentType(final String filename) {
        FileNameMap mapNew = URLConnection.getFileNameMap();
        String temp = mapNew.getContentTypeFor("." + getExtension(filename));
        return (temp == null) ? "application/octet-stream" : temp;
    }

    public static String getExtension(final File f) {

        return getExtension(f.getName());
    }

    public static String getExtension(final String f) {

        return f.substring(f.lastIndexOf('.') + 1);
    }
}

Related

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