convert Extension To MIME Type - Java Network

Java examples for Network:Mime Type

Description

convert Extension To MIME Type

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    /**//from  w  ww  .j av  a 2  s . c  o m
     *
     */
    protected static Map<String, String> fileNameToMIMEType;

    /**
     *
     */
    protected static synchronized String convertExtensionToMIMEType(
            String extension) {
        String res = null;

        {
            if (extension != null) {
                Map<String, String> m = getMapFileNameToMIMEType();

                if (m != null) {
                    res = m.get(extension);
                }
            }
        }

        return res;
    }

    /**
     *
     */
    protected static synchronized Map<String, String> getMapFileNameToMIMEType() {
        Map<String, String> res = null;

        {
            if (fileNameToMIMEType == null) {
                fileNameToMIMEType = new HashMap<String, String>();

                {
                    String extension = "dat";
                    String mimeType = "application/octet-stream";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "txt";
                    String mimeType = "text/plain";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "xml";
                    String mimeType = "text/xml";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "prop";
                    String mimeType = "text/plain";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "html";
                    String mimeType = "text/html";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "xhtml";
                    String mimeType = "application/xhtml+xml";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "zip";
                    String mimeType = "application/octet-stream";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "gz";
                    String mimeType = "application/octet-stream";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "tar";
                    String mimeType = "application/octet-stream";
                    fileNameToMIMEType.put(extension, mimeType);
                }
                {
                    String extension = "rar";
                    String mimeType = "application/octet-stream";
                    fileNameToMIMEType.put(extension, mimeType);
                }
            }

            res = fileNameToMIMEType;
        }

        return res;
    }
}

Related Tutorials