一、前言
关于apache的fulcrum-mimetype的核心源码包定义org.apache.fulcrum.mimetype.util.MimeType文件后缀多实例实现类, 对常用的文件后缀类型进行相关参数注入对象实例进行统一管理等操作。
二、代码示例
1.MimeType类
package org.apache.fulcrum.mimetype.util;@b@@b@import java.util.ArrayList;@b@@b@public class MimeType@b@ implements Cloneable@b@{@b@ public static MimeType TEXT_HTML = new MimeType("text/html");@b@ public static MimeType TEXT_WML = new MimeType("text/vnd.wap.wml");@b@ public static MimeType TEXT_HDML = new MimeType("text/x-hdml");@b@ public static MimeType TEXT_CHTML = new MimeType("text/x-chtml");@b@ public static MimeType TEXT_PLAIN = new MimeType("text/plain");@b@ public static MimeType MULTIPART = new MimeType("multipart/*");@b@ public static MimeType MULTIPART_FORM_DATA = new MimeType("multipart/form-data");@b@ public static MimeType APPLICATION_POSTSCRIPT = new MimeType("application/postscript");@b@ public static MimeType APPLICATION_OCTET_STREAM = new MimeType("application/octet-stream");@b@ public static MimeType APPLICATION_X_JAVA_AGENT = new MimeType("application/x-java-agent");@b@ public static MimeType APPLICATION_X_WWW_FORM_URLENCODED = new MimeType("application/x-www-form-urlencoded");@b@ public static MimeType MESSAGE_HTTP = new MimeType("message/http");@b@ public static MimeType TEXT_CSS = new MimeType("text/css");@b@ public static MimeType TEXT = new MimeType("text/*");@b@ public static MimeType IMAGE_PNG = new MimeType("image/png");@b@ public static MimeType IMAGE_GIF = new MimeType("image/gif");@b@ public static MimeType IMAGE_JPEG = new MimeType("image/jpeg");@b@ public static MimeType IMAGE_WBMP = new MimeType("image/vnd.wap.wbmp");@b@ public static final int NO_MATCH = 0;@b@ public static final int MATCH_TYPE = 1;@b@ public static final int MATCH_SUBTYPE = 2;@b@ public static final int MATCH_SPECIFIC_SUBTYPE = 3;@b@ private String mimeType;@b@ private String mimeSubtype;@b@ private String[] parameterNames;@b@ private String[] parameterValues;@b@ private String mimeTypeString;@b@@b@ public MimeType(String spec)@b@ {@b@ this(spec, true);@b@ }@b@@b@ public MimeType(String spec, boolean parsep)@b@ {@b@ int start = 0;@b@ char look = ';@b@ int length = spec.length();@b@@b@ while ((start < length) && (Character.isWhitespace(spec.charAt(start))))@b@ {@b@ ++start;@b@ }@b@ while ((length > start) && (Character.isWhitespace(spec.charAt(length - 1))))@b@ {@b@ --length;@b@ }@b@@b@ StringBuffer sb = new StringBuffer();@b@ while (start < length) { if ((look = spec.charAt(start)) == '/')@b@ break;@b@@b@ sb.append(look);@b@ ++start;@b@ }@b@ if (look != '/')@b@ {@b@ throw new IllegalArgumentException("Syntax error in MIME type " + spec);@b@ }@b@@b@ this.mimeType = sb.toString();@b@@b@ ++start;@b@ sb.setLength(0);@b@@b@ while (start < length) { if (((look = spec.charAt(start)) == ';') || (Character.isWhitespace(look)))@b@ break;@b@@b@ sb.append(look);@b@ ++start;@b@ }@b@ this.mimeSubtype = sb.toString();@b@@b@ if (parsep)@b@ {@b@ while ((start < length) && (Character.isWhitespace(spec.charAt(start))))@b@ {@b@ ++start;@b@ }@b@ if (start < length)@b@ {@b@ if (spec.charAt(start) != ';')@b@ {@b@ throw new IllegalArgumentException("Syntax error in MIME type parameters " + spec);@b@ }@b@@b@ ++start;@b@ ArrayList na = new ArrayList(4);@b@ ArrayList va = new ArrayList(4);@b@ while (start < length)@b@ {@b@ while ((start < length) && (Character.isWhitespace(spec.charAt(start))))@b@ {@b@ ++start;@b@ }@b@ sb.setLength(0);@b@@b@ while (start < length) { if (((look = spec.charAt(start)) == '=') || (Character.isWhitespace(look)))@b@ break;@b@@b@ sb.append(Character.toLowerCase(look));@b@ ++start;@b@ }@b@ String name = sb.toString();@b@@b@ while ((start < length) && (Character.isWhitespace(spec.charAt(start))))@b@ {@b@ ++start;@b@ }@b@ if (spec.charAt(start) != '=')@b@ {@b@ throw new IllegalArgumentException("Syntax error in MIME type parameters " + spec);@b@ }@b@@b@ ++start;@b@ while ((start < length) && (Character.isWhitespace(spec.charAt(start))))@b@ {@b@ ++start;@b@ }@b@ sb.setLength(0);@b@ char delim = ';';@b@ if (spec.charAt(start) == '"')@b@ {@b@ ++start;@b@ delim = '"';@b@ }@b@@b@ while (start < length) { if (((look = spec.charAt(start)) == delim) || ((delim != '"') && (Character.isWhitespace(look)))) {@b@ break;@b@ }@b@@b@ sb.append(look);@b@ ++start;@b@ }@b@ while ((start < length) && (spec.charAt(start) != ';'))@b@ {@b@ ++start;@b@ }@b@ ++start;@b@ String value = sb.toString();@b@@b@ na.add(name);@b@ va.add(value);@b@ }@b@ this.parameterNames = ((String[])(String[])na.toArray(new String[na.size()]));@b@ this.parameterValues = ((String[])(String[])va.toArray(new String[va.size()]));@b@ }@b@ }@b@ }@b@@b@ public MimeType(String type, String subtype)@b@ {@b@ this(type, subtype, null, null);@b@ }@b@@b@ public MimeType(String type, String subtype, String[] names, String[] values)@b@ {@b@ if ((type == null) || (subtype == null))@b@ {@b@ throw new NullPointerException("MIME type or subtype missing");@b@ }@b@ this.mimeType = type.trim();@b@ this.mimeSubtype = subtype.trim();@b@ this.parameterNames = names;@b@ this.parameterValues = values;@b@ }@b@@b@ public int match(MimeType other)@b@ {@b@ if ((this.mimeType.equals("*")) || (other.mimeType.equals("*")))@b@ {@b@ return 1;@b@ }@b@ if (!(this.mimeType.equalsIgnoreCase(other.mimeType)))@b@ {@b@ return 0;@b@ }@b@ if ((this.mimeSubtype.equals("*")) || (other.mimeSubtype.equals("*")))@b@ {@b@ return 2;@b@ }@b@ if (!(this.mimeSubtype.equalsIgnoreCase(other.mimeSubtype)))@b@ {@b@ return 0;@b@ }@b@@b@ return 3;@b@ }@b@@b@ public String getType()@b@ {@b@ return this.mimeType;@b@ }@b@@b@ public String getSubtype()@b@ {@b@ return this.mimeSubtype;@b@ }@b@@b@ public String getTypes()@b@ {@b@ return this.mimeType + '/' + this.mimeSubtype;@b@ }@b@@b@ public boolean hasParameter(String param)@b@ {@b@ int i;@b@ String[] na = this.parameterNames;@b@ if (na != null)@b@ {@b@ for (i = 0; i < na.length; ++i)@b@ {@b@ if (na[i].equalsIgnoreCase(param))@b@ {@b@ return true;@b@ }@b@ }@b@ }@b@ return false;@b@ }@b@@b@ public String getParameter(String param)@b@ {@b@ String[] va;@b@ int i;@b@ String[] na = this.parameterNames;@b@ if (na != null)@b@ {@b@ va = this.parameterValues;@b@ for (i = 0; i < na.length; ++i)@b@ {@b@ if (na[i].equalsIgnoreCase(param))@b@ {@b@ return va[i];@b@ }@b@ }@b@ }@b@ return null;@b@ }@b@@b@ public synchronized void setParameter(String param, String value)@b@ {@b@ int i;@b@ if (this.parameterNames != null)@b@ {@b@ for (i = 0; i < this.parameterNames.length; ++i)@b@ {@b@ if (this.parameterNames[i].equalsIgnoreCase(param))@b@ {@b@ this.parameterValues[i] = value;@b@ this.mimeTypeString = null;@b@ return;@b@ }@b@ }@b@ }@b@ addParameter(param, value);@b@ }@b@@b@ public void addParameter(String param, String value)@b@ {@b@ addParameters(new String[] { param }, new String[] { value });@b@ }@b@@b@ public synchronized void addParameters(String[] params, String[] values)@b@ {@b@ if ((params == null) || (values == null) || (params.length != values.length))@b@ {@b@ throw new IllegalArgumentException("Incorrect MIME type parameters");@b@ }@b@ if (this.parameterNames != null)@b@ {@b@ String[] na = new String[this.parameterNames.length + params.length];@b@ String[] va = new String[this.parameterValues.length + values.length];@b@ System.arraycopy(this.parameterNames, 0, na, 0, this.parameterNames.length);@b@ System.arraycopy(params, 0, na, this.parameterNames.length, params.length);@b@ System.arraycopy(this.parameterValues, 0, va, 0, this.parameterValues.length);@b@ System.arraycopy(values, 0, va, this.parameterValues.length, values.length);@b@ this.parameterNames = na;@b@ this.parameterValues = va;@b@ }@b@ else@b@ {@b@ this.parameterNames = params;@b@ this.parameterValues = values;@b@ }@b@ this.mimeTypeString = null;@b@ }@b@@b@ public String toString()@b@ {@b@ if (this.mimeTypeString == null)@b@ {@b@ String[] va;@b@ int i;@b@ StringBuffer sb = new StringBuffer(this.mimeType);@b@ sb.append('/');@b@ sb.append(this.mimeSubtype);@b@ String[] na = this.parameterNames;@b@ if (na != null)@b@ {@b@ va = this.parameterValues;@b@ for (i = 0; i < va.length; ++i)@b@ {@b@ sb.append(';');@b@ sb.append(na[i]);@b@ if (va[i] != null)@b@ {@b@ sb.append('=');@b@ sb.append(va[i]);@b@ }@b@ }@b@ }@b@ this.mimeTypeString = sb.toString();@b@ }@b@ return this.mimeTypeString;@b@ }@b@}
2.常用文件后缀类型对照表
final String[][] MIME_MapTable={ @b@ //{后缀名,MIME类型} @b@ {".3gp", "video/3gpp"}, @b@ {".apk", "application/vnd.android.package-archive"}, @b@ {".asf", "video/x-ms-asf"}, @b@ {".avi", "video/x-msvideo"}, @b@ {".bin", "application/octet-stream"}, @b@ {".bmp", "image/bmp"}, @b@ {".c", "text/plain"}, @b@ {".class", "application/octet-stream"}, @b@ {".conf", "text/plain"}, @b@ {".cpp", "text/plain"}, @b@ {".doc", "application/msword"}, @b@ {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, @b@ {".xls", "application/vnd.ms-excel"}, @b@ {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, @b@ {".exe", "application/octet-stream"}, @b@ {".gif", "image/gif"}, @b@ {".gtar", "application/x-gtar"}, @b@ {".gz", "application/x-gzip"}, @b@ {".h", "text/plain"}, @b@ {".htm", "text/html"}, @b@ {".html", "text/html"}, @b@ {".jar", "application/java-archive"}, @b@ {".java", "text/plain"}, @b@ {".jpeg", "image/jpeg"}, @b@ {".jpg", "image/jpeg"}, @b@ {".js", "application/x-javascript"}, @b@ {".log", "text/plain"}, @b@ {".m3u", "audio/x-mpegurl"}, @b@ {".m4a", "audio/mp4a-latm"}, @b@ {".m4b", "audio/mp4a-latm"}, @b@ {".m4p", "audio/mp4a-latm"}, @b@ {".m4u", "video/vnd.mpegurl"}, @b@ {".m4v", "video/x-m4v"}, @b@ {".mov", "video/quicktime"}, @b@ {".mp2", "audio/x-mpeg"}, @b@ {".mp3", "audio/x-mpeg"}, @b@ {".mp4", "video/mp4"}, @b@ {".mpc", "application/vnd.mpohun.certificate"}, @b@ {".mpe", "video/mpeg"}, @b@ {".mpeg", "video/mpeg"}, @b@ {".mpg", "video/mpeg"}, @b@ {".mpg4", "video/mp4"}, @b@ {".mpga", "audio/mpeg"}, @b@ {".msg", "application/vnd.ms-outlook"}, @b@ {".ogg", "audio/ogg"}, @b@ {".pdf", "application/pdf"}, @b@ {".png", "image/png"}, @b@ {".pps", "application/vnd.ms-powerpoint"}, @b@ {".ppt", "application/vnd.ms-powerpoint"}, @b@ {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, @b@ {".prop", "text/plain"}, @b@ {".rc", "text/plain"}, @b@ {".rmvb", "audio/x-pn-realaudio"}, @b@ {".rtf", "application/rtf"}, @b@ {".sh", "text/plain"}, @b@ {".tar", "application/x-tar"}, @b@ {".tgz", "application/x-compressed"}, @b@ {".txt", "text/plain"}, @b@ {".wav", "audio/x-wav"}, @b@ {".wma", "audio/x-ms-wma"}, @b@ {".wmv", "audio/x-ms-wmv"}, @b@ {".wps", "application/vnd.ms-works"}, @b@ {".xml", "text/plain"}, @b@ {".z", "application/x-compress"}, @b@ {".zip", "application/x-zip-compressed"}, @b@ {"", "*/*"} @b@ };