contenttypehandler.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 98 行

SVN-BASE
98
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.cldc.communication.midp;import java.io.File;import java.util.Hashtable;/** * This class stores file extension - content-type pairs and * provides convenient content-type related methods. */public class ContentTypeHandler {    private Hashtable contentTypesTable;    private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";        public ContentTypeHandler() {        contentTypesTable = new Hashtable();        registerExtension("jar", "application/java-archive");        registerExtension("wav", "audio/x-wav");        registerExtension("mid", "audio/midi");        registerExtension("wrng", "wrong/mediatype");        registerExtension("svg", "image/svg");        registerExtension("amr", "audio/amr");        registerExtension("gif", "image/gif");        registerExtension("wbmp",  "image/vnd.wap.wbmp");        registerExtension("png", "image/png");        registerExtension("jpg", "image/jpeg");    }    public void registerExtension(String ext, String type) {        contentTypesTable.put(ext.toLowerCase(), type);    }    /**     * Parses the string and registers found extension/content-type pairs.     * @param string String with format "ext1=cont/type1;ext2=cont/type2".     * @throws IllegalArgumentException If string's format is invalid.     */    public void registerExtensions(String string) throws IllegalArgumentException {        String[] pairs = string.split(";");        for (int i=0; i < pairs.length; i++) {            String[] pair = pairs[i].split("=");            if (pair.length != 2) {                throw new IllegalArgumentException(                        "Invalid string format: " + string);            } else {                registerExtension(pair[0], pair[1]);            }        }    }    public String getContentType(File file) {            String ext = getFileExtension(file).toLowerCase();            String cType = (String) contentTypesTable.get(ext);            if (cType != null) {                return cType;            } else {                return DEFAULT_CONTENT_TYPE;            }    }        private static String getFileExtension(File file) {        String fileName = file.getName();        int extIndex = fileName.lastIndexOf(".");        if (extIndex != -1) {            return fileName.substring(extIndex + 1);        } else {            return "";        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?