Returns the mime-type according to the given file extension. - Java Network

Java examples for Network:Mime Type

Description

Returns the mime-type according to the given file extension.

Demo Code


//package com.java2s;

public class Main {
    /**/* w  w w.j  a v a 2s  .  c o m*/
     * Returns the mime-type according to the given file extension.
     * Default is application/octet-stream.
     * @param extension
     * @return mime-type
     */
    public static String guessMimeType(String extension) {
        if (extension != null) {
            String ext = extension.toLowerCase();

            if (ext.equals("css"))
                return "text/css";
            if (ext.equals("doc"))
                return "application/msword";

            if (ext.equals("html") || ext.equals("htm"))
                return "text/html";

            if (ext.equals("gif"))
                return "image/gif";
            if (ext.equals("jpg") || ext.equals("jpeg"))
                return "image/jpeg";
            if (ext.equals("bmp"))
                return "image/bmp";
            if (ext.equals("tiff") || ext.equals("tif"))
                return "image/tiff";
            if (ext.equals("png"))
                return "image/png";

            if (ext.equals("js"))
                return "application/x-javascript";
            if (ext.equals("mov"))
                return "video/quicktime";
            if (ext.equals("odt"))
                return "application/vnd.oasis.opendocument.text";
            if (ext.equals("pdf"))
                return "application/pdf";
            if (ext.equals("sxc"))
                return "application/vnd.sun.xml.calc";
            if (ext.equals("txt"))
                return "text/plain";
            if (ext.equals("xhtml"))
                return "application/xhtml+xml";
            if (ext.equals("xml"))
                return "application/xml";
            if (ext.equals("xpi"))
                return "application/x-xpinstall";
            if (ext.equals("zip"))
                return "application/zip";
            // TODO: add more
        }
        return "application/octet-stream"; // default
    }
}

Related Tutorials