集册 Java实例教程 将扩展名转换为MIME类型

将扩展名转换为MIME类型

欢马劈雪     最近更新时间:2020-01-02 10:19:05

438
将扩展名转换为MIME类型


//package com.nowjava;

import java.util.*;
/*N o w J a v a . c o m - 时  代  Java 提 供*/

public class Main {

    /**

     *

     */

    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);/*NowJava.com 提供*/

                }

            }

        }


        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);

                }

          
展开阅读全文