Java/Network Protocol/MIME

Материал из Java эксперт
Перейти к: навигация, поиск

A convenience class which handles conversions between MIME charset names and Java encoding names.

   <source lang="java">

import java.util.*; /**

* MIME2Java is a convenience class which handles conversions between MIME charset names
* and Java encoding names.
*

The supported XML encodings are the intersection of XML-supported code sets and those * supported in JDK 1.1. * <p>MIME charset names are used on xmlEncoding parameters to methods such * as TXDocument#setEncoding and DTD#setEncoding. * <p>Java encoding names are used on encoding parameters to * methods such as TXDocument#printWithFormat and DTD#printExternal. * <P> *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*

Common Name * </TD> *

*          <P ALIGN="CENTER">Use this name in XML files
*      </TD>
*
*          <P ALIGN="CENTER">Name Type
*      </TD>
*
*          <P ALIGN="CENTER">Xerces converts to this Java Encoder Name
*      </TD>
*  </TR>
*
8 bit Unicode</TD> *
*          <P ALIGN="CENTER">UTF-8
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">UTF8
*      </TD>
*  </TR>
*
ISO Latin 1</TD> *
*          <P ALIGN="CENTER">ISO-8859-1
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-1
*      </TD>
*  </TR>
*
ISO Latin 2</TD> *
*          <P ALIGN="CENTER">ISO-8859-2
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-2
*      </TD>
*  </TR>
*
ISO Latin 3</TD> *
*          <P ALIGN="CENTER">ISO-8859-3
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-3
*      </TD>
*  </TR>
*
ISO Latin 4</TD> *
*          <P ALIGN="CENTER">ISO-8859-4
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-4
*      </TD>
*  </TR>
*
ISO Latin Cyrillic</TD> *
*          <P ALIGN="CENTER">ISO-8859-5
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-5
*      </TD>
*  </TR>
*
ISO Latin Arabic</TD> *
*          <P ALIGN="CENTER">ISO-8859-6
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-6
*      </TD>
*  </TR>
*
ISO Latin Greek</TD> *
*          <P ALIGN="CENTER">ISO-8859-7
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-7
*      </TD>
*  </TR>
*
ISO Latin Hebrew</TD> *
*          <P ALIGN="CENTER">ISO-8859-8
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-8
*      </TD>
*  </TR>
*
ISO Latin 5</TD> *
*          <P ALIGN="CENTER">ISO-8859-9
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">ISO-8859-9
*      </TD>
*  </TR>
*
EBCDIC: US</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-us
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp037
*      </TD>
*  </TR>
*
EBCDIC: Canada</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-ca
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp037
*      </TD>
*  </TR>
*
EBCDIC: Netherlands</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-nl
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp037
*      </TD>
*  </TR>
*
EBCDIC: Denmark</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-dk
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp277
*      </TD>
*  </TR>
*
EBCDIC: Norway</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-no
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp277
*      </TD>
*  </TR>
*
EBCDIC: Finland</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-fi
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp278
*      </TD>
*  </TR>
*
EBCDIC: Sweden</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-se
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp278
*      </TD>
*  </TR>
*
EBCDIC: Italy</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-it
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp280
*      </TD>
*  </TR>
*
EBCDIC: Spain, Latin America</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-es
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp284
*      </TD>
*  </TR>
*
EBCDIC: Great Britain</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-gb
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp285
*      </TD>
*  </TR>
*
EBCDIC: France</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-fr
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp297
*      </TD>
*  </TR>
*
EBCDIC: Arabic</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-ar1
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp420
*      </TD>
*  </TR>
*
EBCDIC: Hebrew</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-he
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp424
*      </TD>
*  </TR>
*
EBCDIC: Switzerland</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-ch
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp500
*      </TD>
*  </TR>
*
EBCDIC: Roece</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-roece
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp870
*      </TD>
*  </TR>
*
EBCDIC: Yogoslavia</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-yu
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp870
*      </TD>
*  </TR>
*
EBCDIC: Iceland</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-is
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp871
*      </TD>
*  </TR>
*
EBCDIC: Urdu</TD> *
*          <P ALIGN="CENTER">ebcdic-cp-ar2
*      </TD>
*
*          <P ALIGN="CENTER">IANA
*      </TD>
*
*          <P ALIGN="CENTER">cp918
*      </TD>
*  </TR>
*
Chinese for PRC, mixed 1/2 byte</TD> *
*          <P ALIGN="CENTER">gb2312
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">GB2312
*      </TD>
*  </TR>
*
Extended Unix Code, packed for Japanese</TD> *
*          <P ALIGN="CENTER">euc-jp
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">eucjis
*      </TD>
*  </TR>
*
Japanese: iso-2022-jp</TD> *
*          <P ALIGN="CENTER">iso-2020-jp
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">JIS
*      </TD>
*  </TR>
*
Japanese: Shift JIS</TD> *
*          <P ALIGN="CENTER">Shift_JIS
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">SJIS
*      </TD>
*  </TR>
*
Chinese: Big5</TD> *
*          <P ALIGN="CENTER">Big5
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">Big5
*      </TD>
*  </TR>
*
Extended Unix Code, packed for Korean</TD> *
*          <P ALIGN="CENTER">euc-kr
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">iso2022kr
*      </TD>
*  </TR>
*
Cyrillic</TD> *
*          <P ALIGN="CENTER">koi8-r
*      </TD>
*
*          <P ALIGN="CENTER">MIME
*      </TD>
*
*          <P ALIGN="CENTER">koi8-r
*      </TD>
*  </TR>
* </TABLE>
*
* @version $Revision: 515 $ $Date: 2008-03-17 22:02:23 +0100 (Mon, 17 Mar 2008) $
* @author TAMURA Kent <kent@trl.ibm.co.jp>
*/

public class MIME2Java {

   static private Hashtable s_enchash;
   static private Hashtable s_revhash;
   static {
       s_enchash = new Hashtable();
       //    <preferred MIME name>, <Java encoding name>
       s_enchash.put("UTF-8", "UTF8");
       s_enchash.put("US-ASCII",        "8859_1");    // ?
       s_enchash.put("ISO-8859-1",      "8859_1");
       s_enchash.put("ISO-8859-2",      "8859_2");
       s_enchash.put("ISO-8859-3",      "8859_3");
       s_enchash.put("ISO-8859-4",      "8859_4");
       s_enchash.put("ISO-8859-5",      "8859_5");
       s_enchash.put("ISO-8859-6",      "8859_6");
       s_enchash.put("ISO-8859-7",      "8859_7");
       s_enchash.put("ISO-8859-8",      "8859_8");
       s_enchash.put("ISO-8859-9",      "8859_9");
       s_enchash.put("ISO-2022-JP",     "JIS");
       s_enchash.put("SHIFT_JIS",       "SJIS");
       s_enchash.put("EUC-JP",          "EUCJIS");
       s_enchash.put("GB2312",          "GB2312");
       s_enchash.put("BIG5",            "Big5");
       s_enchash.put("EUC-KR",          "KSC5601");
       s_enchash.put("ISO-2022-KR",     "ISO2022KR");
       s_enchash.put("KOI8-R",          "KOI8_R");
       s_enchash.put("EBCDIC-CP-US",    "CP037");
       s_enchash.put("EBCDIC-CP-CA",    "CP037");
       s_enchash.put("EBCDIC-CP-NL",    "CP037");
       s_enchash.put("EBCDIC-CP-DK",    "CP277");
       s_enchash.put("EBCDIC-CP-NO",    "CP277");
       s_enchash.put("EBCDIC-CP-FI",    "CP278");
       s_enchash.put("EBCDIC-CP-SE",    "CP278");
       s_enchash.put("EBCDIC-CP-IT",    "CP280");
       s_enchash.put("EBCDIC-CP-ES",    "CP284");
       s_enchash.put("EBCDIC-CP-GB",    "CP285");
       s_enchash.put("EBCDIC-CP-FR",    "CP297");
       s_enchash.put("EBCDIC-CP-AR1",   "CP420");
       s_enchash.put("EBCDIC-CP-HE",    "CP424");
       s_enchash.put("EBCDIC-CP-CH",    "CP500");
       s_enchash.put("EBCDIC-CP-ROECE", "CP870");
       s_enchash.put("EBCDIC-CP-YU",    "CP870");
       s_enchash.put("EBCDIC-CP-IS",    "CP871");
       s_enchash.put("EBCDIC-CP-AR2",   "CP918");
                                               // j:CNS11643 -> EUC-TW?
                                               // ISO-2022-CN? ISO-2022-CN-EXT?
       s_revhash = new Hashtable();
       //    <Java encoding name>, <preferred MIME name>
       s_revhash.put("UTF8", "UTF-8");
       //s_revhash.put("8859_1", "US-ASCII");    // ?
       s_revhash.put("8859_1", "ISO-8859-1");
       s_revhash.put("8859_2", "ISO-8859-2");
       s_revhash.put("8859_3", "ISO-8859-3");
       s_revhash.put("8859_4", "ISO-8859-4");
       s_revhash.put("8859_5", "ISO-8859-5");
       s_revhash.put("8859_6", "ISO-8859-6");
       s_revhash.put("8859_7", "ISO-8859-7");
       s_revhash.put("8859_8", "ISO-8859-8");
       s_revhash.put("8859_9", "ISO-8859-9");
       s_revhash.put("JIS", "ISO-2022-JP");
       s_revhash.put("SJIS", "Shift_JIS");
       s_revhash.put("EUCJIS", "EUC-JP");
       s_revhash.put("GB2312", "GB2312");
       s_revhash.put("BIG5", "Big5");
       s_revhash.put("KSC5601", "EUC-KR");
       s_revhash.put("ISO2022KR", "ISO-2022-KR");
       s_revhash.put("KOI8_R", "KOI8-R");
       s_revhash.put("CP037", "EBCDIC-CP-US");
       s_revhash.put("CP037", "EBCDIC-CP-CA");
       s_revhash.put("CP037", "EBCDIC-CP-NL");
       s_revhash.put("CP277", "EBCDIC-CP-DK");
       s_revhash.put("CP277", "EBCDIC-CP-NO");
       s_revhash.put("CP278", "EBCDIC-CP-FI");
       s_revhash.put("CP278", "EBCDIC-CP-SE");
       s_revhash.put("CP280", "EBCDIC-CP-IT");
       s_revhash.put("CP284", "EBCDIC-CP-ES");
       s_revhash.put("CP285", "EBCDIC-CP-GB");
       s_revhash.put("CP297", "EBCDIC-CP-FR");
       s_revhash.put("CP420", "EBCDIC-CP-AR1");
       s_revhash.put("CP424", "EBCDIC-CP-HE");
       s_revhash.put("CP500", "EBCDIC-CP-CH");
       s_revhash.put("CP870", "EBCDIC-CP-ROECE");
       s_revhash.put("CP870", "EBCDIC-CP-YU");
       s_revhash.put("CP871", "EBCDIC-CP-IS");
       s_revhash.put("CP918", "EBCDIC-CP-AR2");
   }
   private MIME2Java() {
   }
   /**
    * Convert a MIME charset name, also known as an XML encoding name, to a Java encoding name.
    * @param   mimeCharsetName Case insensitive MIME charset name: UTF-8, US-ASCII, ISO-8859-1,
    *                          ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6,
    *                          ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-2022-JP, Shift_JIS,
    *                          EUC-JP, GB2312, Big5, EUC-KR, ISO-2022-KR, KOI8-R,
    *                          EBCDIC-CP-US, EBCDIC-CP-CA, EBCDIC-CP-NL, EBCDIC-CP-DK,
    *                          EBCDIC-CP-NO, EBCDIC-CP-FI, EBCDIC-CP-SE, EBCDIC-CP-IT,
    *                          EBCDIC-CP-ES, EBCDIC-CP-GB, EBCDIC-CP-FR, EBCDIC-CP-AR1,
    *                          EBCDIC-CP-HE, EBCDIC-CP-CH, EBCDIC-CP-ROECE, EBCDIC-CP-YU,
    *                          EBCDIC-CP-IS and EBCDIC-CP-AR2.
    * @return                  Java encoding name, or null if mimeCharsetName
    *                          is unknown.
    * @see #reverse
    */
   public static String convert(String mimeCharsetName) {
       return (String)s_enchash.get(mimeCharsetName.toUpperCase());
   }
   /**
    * Convert a Java encoding name to MIME charset name.
    * Available values of encoding are "UTF8", "8859_1", "8859_2", "8859_3", "8859_4",
    * "8859_5", "8859_6", "8859_7", "8859_8", "8859_9", "JIS", "SJIS", "EUCJIS",
    * "GB2312", "BIG5", "KSC5601", "ISO2022KR",  "KOI8_R", "CP037", "CP277", "CP278",
    * "CP280", "CP284", "CP285", "CP297", "CP420", "CP424", "CP500", "CP870", "CP871" and "CP918".
    * @param   encoding    Case insensitive Java encoding name: UTF8, 8859_1, 8859_2, 8859_3,
    *                      8859_4, 8859_5, 8859_6, 8859_7, 8859_8, 8859_9, JIS, SJIS, EUCJIS,
    *                      GB2312, BIG5, KSC5601, ISO2022KR, KOI8_R, CP037, CP277, CP278,
    *                      CP280, CP284, CP285, CP297, CP420, CP424, CP500, CP870, CP871
    *                      and CP918.
    * @return              MIME charset name, or null if encoding is unknown.
    * @see #convert
    */
   public static String reverse(String encoding) {
       return (String)s_revhash.get(encoding.toUpperCase());
   }

}

 </source>
   
  
 
  



A utility class, which provides some MIME related application logic.

   <source lang="java">

/****************************************************************

* Licensed to the Apache Software Foundation (ASF) under one   *
* or more contributor license agreements.  See the NOTICE file *
* distributed with this work for additional information        *
* regarding copyright ownership.  The ASF licenses this file   *
* to you under the Apache License, Version 2.0 (the            *
* "License"); you may not use this file except in compliance   *
* with the License.  You may obtain a copy of the License at   *
*                                                              *
*   http://www.apache.org/licenses/LICENSE-2.0                 *
*                                                              *
* Unless required by applicable law or agreed to in writing,   *
* software distributed under the License is distributed on an  *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
* KIND, either express or implied.  See the License for the    *
* specific language governing permissions and limitations      *
* under the License.                                           *
****************************************************************/

import java.text.DateFormat; import java.text.FieldPosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Random; import java.util.TimeZone;

/**

* A utility class, which provides some MIME related application logic.
*/

public final class MimeUtil {

   /**
    * The quoted-printable encoding.
    */
   public static final String ENC_QUOTED_PRINTABLE = "quoted-printable";
   /**
    * The binary encoding.
    */
   public static final String ENC_BINARY = "binary";
   /**
    * The base64 encoding.
    */
   public static final String ENC_BASE64 = "base64";
   /**
    * The 8bit encoding.
    */
   public static final String ENC_8BIT = "8bit";
   /**
    * The 7bit encoding.
    */
   public static final String ENC_7BIT = "7bit";
   /** MIME-Version header name (lowercase) */
   public static final String MIME_HEADER_MIME_VERSION = "mime-version";
   /** Content-ID header name (lowercase) */
   public static final String MIME_HEADER_CONTENT_ID = "content-id";
   /** Content-Description header name (lowercase) */
   public static final String MIME_HEADER_CONTENT_DESCRIPTION = "content-description";
   /** 
    * Content-Disposition header name (lowercase). 
    * See  section 2). If the string contains non-whitespace sequences
    * longer than 76 characters a line break is inserted at the whitespace
    * character following the sequence resulting in a line longer than 76
    * characters.
    * 
    * @param s
    *            string to split.
    * @param usedCharacters
    *            number of characters already used up. Usually the number of
    *            characters for header field name plus colon and one space.
    * @return a multiple-line representation of the given string.
    */
   public static String fold(String s, int usedCharacters) {
       final int maxCharacters = 76;
       final int length = s.length();
       if (usedCharacters + length <= maxCharacters)
           return s;
       StringBuilder sb = new StringBuilder();
       int lastLineBreak = -usedCharacters;
       int wspIdx = indexOfWsp(s, 0);
       while (true) {
           if (wspIdx == length) {
               sb.append(s.substring(Math.max(0, lastLineBreak)));
               return sb.toString();
           }
           int nextWspIdx = indexOfWsp(s, wspIdx + 1);
           if (nextWspIdx - lastLineBreak > maxCharacters) {
               sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx));
               sb.append("\r\n");
               lastLineBreak = wspIdx;
           }
           wspIdx = nextWspIdx;
       }
   }
   /**
    * Unfold a multiple-line representation into a single line.
    * 
    * @param s
    *            string to unfold.
    * @return unfolded string.
    */
   public static String unfold(String s) {
       final int length = s.length();
       for (int idx = 0; idx < length; idx++) {
           char c = s.charAt(idx);
           if (c == "\r" || c == "\n") {
               return unfold0(s, idx);
           }
       }
       return s;
   }
   private static String unfold0(String s, int crlfIdx) {
       final int length = s.length();
       StringBuilder sb = new StringBuilder(length);
       if (crlfIdx > 0) {
           sb.append(s.substring(0, crlfIdx));
       }
       for (int idx = crlfIdx + 1; idx < length; idx++) {
           char c = s.charAt(idx);
           if (c != "\r" && c != "\n") {
               sb.append(c);
           }
       }
       return sb.toString();
   }
   private static int indexOfWsp(String s, int fromIndex) {
       final int len = s.length();
       for (int index = fromIndex; index < len; index++) {
           char c = s.charAt(index);
           if (c == " " || c == "\t")
               return index;
       }
       return len;
   }
   private static synchronized int nextCounterValue() {
       return counter++;
   }
   private static final ThreadLocal<DateFormat> RFC822_DATE_FORMAT = new ThreadLocal<DateFormat>() {
       @Override
       protected DateFormat initialValue() {
           return new Rfc822DateFormat();
       }
   };
   private static final class Rfc822DateFormat extends SimpleDateFormat {
       private static final long serialVersionUID = 1L;
       public Rfc822DateFormat() {
           super("EEE, d MMM yyyy HH:mm:ss ", Locale.US);
       }
       @Override
       public StringBuffer format(Date date, StringBuffer toAppendTo,
               FieldPosition pos) {
           StringBuffer sb = super.format(date, toAppendTo, pos);
           int zoneMillis = calendar.get(GregorianCalendar.ZONE_OFFSET);
           int dstMillis = calendar.get(GregorianCalendar.DST_OFFSET);
           int minutes = (zoneMillis + dstMillis) / 1000 / 60;
           if (minutes < 0) {
               sb.append("-");
               minutes = -minutes;
           } else {
               sb.append("+");
           }
           sb.append(String.format("%02d%02d", minutes / 60, minutes % 60));
           return sb;
       }
   }

}

 </source>
   
  
 
  



Declaring MIME types to use for various requests and provide utility manipulation methods

   <source lang="java">

/*

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**

*
* <p>Utility class for declaring MIME types to use for various requests and provide
* utility manipulation methods.

*

Added Content-Encoding capability, with defaults * * @author * @version $Id: MimeType.java 516448 2007-03-09 16:25:47Z ate $ */ public class MimeType { public static final MimeType HTML = new MimeType("text/html", "UTF-8"); //FIXME: test public static final MimeType XHTML = new MimeType("text/xhtml"); public static final MimeType WML = new MimeType("text/vnd.wap.wml"); public static final MimeType XML = new MimeType("text/xml"); public static final MimeType VXML = new MimeType("text/vxml"); /** * Standard ContentType String, with no encoding appended. */ private String mimeType = ""; /** * null value means default encoding. * Otherwise, charset to be used. */ private String charSet = null; public MimeType(String mimeType) { if (mimeType == null) { throw new NullPointerException(); } this.mimeType = mimeType; } /** * */ public MimeType(String mimeType, String charSet) { if (mimeType == null) { throw new NullPointerException(); } this.mimeType = mimeType; this.charSet = charSet; } /** Extracts from this MimeType a user-friendly identifying code * ie "html" for "text/html" or "wml" for "text/vnd.wap.wml" * * @return the simplified type */ public String getCode() { String type = this.mimeType; // get everything after "/" type = type.substring(type.indexOf("/") + 1); // remove any dot in the name int idx = type.lastIndexOf("."); if (idx >= 0) { type = type.substring(idx + 1); } //remove anything before a "-" idx = type.lastIndexOf("-"); if (idx >= 0) { type = type.substring(idx + 1); } return type.toLowerCase(); } /** * Return the media type associated */ public String getContentType() { return this.mimeType; } /** * Return the character encoding associated, if any */ public String getCharSet() { return this.charSet; } /** * Convert this MimeType to its external String representation */ public String toString() { if (null == this.charSet) { return this.mimeType; } return this.mimeType + "; charset=" + this.charSet; } /** * Compare one MimeType to another */ public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof MimeType) { MimeType comp = (MimeType) obj; return this.toString().equals(comp.toString()); } else { return false; } } } </source>

Frequently used MIME types

   <source lang="java">

/*

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* $Id: MimeConstants.java 606580 2007-12-23 17:45:02Z jeremias $ */

/**

* Frequently used MIME types for various file formats used by the XML Graphics project.
*/

public interface MimeConstants {

   /** Portable Document Format */
   String MIME_PDF             = "application/pdf";
   
   /** PostScript */
   String MIME_POSTSCRIPT      = "application/postscript";
   /** Encapsulated PostScript (same MIME type as PostScript) */
   String MIME_EPS             = MIME_POSTSCRIPT;
   
   /** HP"s PCL */
   String MIME_PCL             = "application/x-pcl";
   /** HP"s PCL (alternative MIME type) */
   String MIME_PCL_ALT         = "application/vnd.hp-PCL";
   
   /** IBM"s AFP */
   String MIME_AFP             = "application/x-afp";
   /** IBM"s AFP (alternative MIME type) */
   String MIME_AFP_ALT         = "application/vnd.ibm.modcap";
   
   /** Plain text */
   String MIME_PLAIN_TEXT      = "text/plain";
   
   /** Rich text format */
   String MIME_RTF             = "application/rtf";
   /** Rich text format (alternative 1) */
   String MIME_RTF_ALT1        = "text/richtext";
   /** Rich text format (alternative 2) */
   String MIME_RTF_ALT2        = "text/rtf";
   
   /** FrameMaker"s MIF */
   String MIME_MIF             = "application/mif";
   
   /** Structured Vector Graphics */
   String MIME_SVG             = "image/svg+xml";
   
   /** GIF images */
   String MIME_GIF             = "image/gif";
   /** PNG images */
   String MIME_PNG             = "image/png";
   /** JPEG images */
   String MIME_JPEG            = "image/jpeg";
   /** TIFF images */
   String MIME_TIFF            = "image/tiff";
      
   /** Proposed but non-registered MIME type for XSL-FO */
   String MIME_XSL_FO          = "text/xsl";
   

}

 </source>
   
  
 
  



HTTP MIME Response

   <source lang="java">

//Revised from jcommon web;

import java.io.*; import java.net.*; import java.util.*;

public class HTTPResponse {

 public static final HashMap<String,String> MIME_TYPES = new HashMap<String,String>();
 static {
   MIME_TYPES.put("gif", "image/gif");
   MIME_TYPES.put("jpeg", "image/jpeg");
   MIME_TYPES.put("jpg", "image/jpeg");
   MIME_TYPES.put("jpe", "image/jpeg");
   MIME_TYPES.put("bmp", "image/bmp");
   MIME_TYPES.put("png", "image/png");
   MIME_TYPES.put("tif", "image/tiff");
   MIME_TYPES.put("tiff", "image/tiff");
   MIME_TYPES.put("jnlp", "application/x-java-jnlp-file");
   MIME_TYPES.put("js", "application/x-javascript");
   MIME_TYPES.put("doc", "application/msword");
   MIME_TYPES.put("bin", "application/octet-stream");
   MIME_TYPES.put("exe", "application/octet-stream");
   MIME_TYPES.put("pdf", "application/pdf");
   MIME_TYPES.put("ai", "application/postscript");
   MIME_TYPES.put("eps", "application/postscript");
   MIME_TYPES.put("ps", "application/postscript");
   MIME_TYPES.put("rtf", "application/rtf");
   MIME_TYPES.put("class", "application/x-java-vm");
   MIME_TYPES.put("ser", "application/x-java-serialized-object");
   MIME_TYPES.put("jar", "application/x-java-archive");
   MIME_TYPES.put("sh", "application/x-sh");
   MIME_TYPES.put("tar", "application/x-tar");
   MIME_TYPES.put("zip", "application/zip");
   MIME_TYPES.put("ua", "audio/basic");
   MIME_TYPES.put("wav", "audio/x-wav");
   MIME_TYPES.put("mid", "audio/x-midi");
   MIME_TYPES.put("htm", "text/html");
   MIME_TYPES.put("html", "text/html");
   MIME_TYPES.put("css", "text/css");
   MIME_TYPES.put("txt", "text/plain");
   MIME_TYPES.put("mpeg", "video/mpeg");
   MIME_TYPES.put("mpg", "video/mpeg");
   MIME_TYPES.put("mpe", "video/mpeg");
   MIME_TYPES.put("qt", "video/quicktime");
   MIME_TYPES.put("mov", "video/quicktime");
   MIME_TYPES.put("avi", "video/avi");
   MIME_TYPES.put("movie", "video/x-sgi-movie");
 }
 
 public static String SERVER = "JavaWebServer/1.0";
 
 public static final int OK = 200;
 
 private Socket s;
 private int mode;
 private HashMap<String,String> headers;
 private HashSet<String> keys;
 
 public HTTPResponse(Socket s, int mode) throws IOException {
   this.s = s;
   this.mode = mode;
   
   headers = new HashMap<String,String>();
   keys = new HashSet<String>();
 }
 
 public void addHeader(String header, String value) {
   keys.add(header.toLowerCase());
   headers.put(header, value);
 }
 
 private void writeHeaders() throws IOException {
   if (mode == OK) {
     writeLine("HTTP/1.1 200 OK");
   }
   if (!keys.contains("server")) {
     headers.put("Server", SERVER);
   }
   if (!keys.contains("date")) {
     headers.put("Date", String.format("%EEE%, %d% %MMM% %yyyy% %HH%:%mm%:%ss% %Z%",new GregorianCalendar()));
   }
   
   Iterator<String> iterator = headers.keySet().iterator();
   String key;
   String value;
   while (iterator.hasNext()) {
     key = iterator.next();
     value = headers.get(key);
     writeLine(key + ": " + value);
   }
   
   writeLine("");
 }
 
 private void writeLine(String string) throws IOException {
   s.getOutputStream().write((string + "\r\n").getBytes());
 }
 
 public void writeFile(File f) throws IOException {
   if ((!keys.contains("content-type")) && (f.getName().indexOf(".") > -1)) {
     String ext = f.getName().substring(f.getName().lastIndexOf(".") + 1).toLowerCase();
     if (MIME_TYPES.containsKey(ext)) {
       headers.put("Content-Type", MIME_TYPES.get(ext));
     }
   }
   if (!keys.contains("content-length")) {
     headers.put("Content-Length", String.valueOf(f.length()));
   }
   writeHeaders();
   
   FileInputStream fis = new FileInputStream(f);
   byte[] b = new byte[512];
   int len;
   while ((len = fis.read(b)) > -1) {
     s.getOutputStream().write(b, 0, len);
   }
   s.getOutputStream().flush();
   s.close();
 }

}

 </source>
   
  
 
  



Known mime types

   <source lang="java">

/*

* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
* 
* Project: OpenSubsystems
* 
* $Id: MimeTypeConstants.java,v 1.6 2007/01/07 06:14:01 bastafidli Exp $
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License. 
* 
* 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 for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
*/

import java.util.HashMap; import java.util.Map; /**

* Known mime types.
* Originally copied from HandlerContext.java taken from Jetty 3.0.4 
* now updated from mime.properties from Jetty 5.1.10.
* 
* @version $Id: MimeTypeConstants.java,v 1.6 2007/01/07 06:14:01 bastafidli Exp $
* @author Julo Legeny
* @code.reviewer Miro Halas
* @code.reviewed 1.4 2005/07/29 07:36:24 bastafidli
*/

public final class MimeTypeConstants {

  // Attributes ///////////////////////////////////////////////////////////////
  
  /**
   * Default Mime type
   */
  protected static String s_strDefaultMimeType = "text/html";
  /**
   * Hash map that will be stored types in
   */
  protected static Map s_mapMimeTypes = null;
  
  // Constructors /////////////////////////////////////////////////////////////
  
  /**
   * Static initializer;
   */
  static 
  {
     s_mapMimeTypes = new HashMap(161);
     s_mapMimeTypes.put("ai", "application/postscript");
     s_mapMimeTypes.put("aif", "audio/x-aiff");
     s_mapMimeTypes.put("aifc", "audio/x-aiff");
     s_mapMimeTypes.put("aiff", "audio/x-aiff");
     s_mapMimeTypes.put("asc", "text/plain");
     s_mapMimeTypes.put("asf", "video/x.ms.asf");
     s_mapMimeTypes.put("asx", "video/x.ms.asx");
     s_mapMimeTypes.put("au", "audio/basic");
     s_mapMimeTypes.put("avi", "video/x-msvideo");
     s_mapMimeTypes.put("bcpio", "application/x-bcpio");
     s_mapMimeTypes.put("bin", "application/octet-stream");
     s_mapMimeTypes.put("cab", "application/x-cabinet");
     s_mapMimeTypes.put("cdf", "application/x-netcdf");
     s_mapMimeTypes.put("class", "application/java-vm");
     s_mapMimeTypes.put("cpio", "application/x-cpio");
     s_mapMimeTypes.put("cpt", "application/mac-compactpro");
     s_mapMimeTypes.put("crt", "application/x-x509-ca-cert");
     s_mapMimeTypes.put("csh", "application/x-csh");
     s_mapMimeTypes.put("css", "text/css");
     s_mapMimeTypes.put("csv", "text/comma-separated-values");
     s_mapMimeTypes.put("dcr", "application/x-director");
     s_mapMimeTypes.put("dir", "application/x-director");
     s_mapMimeTypes.put("dll", "application/x-msdownload");
     s_mapMimeTypes.put("dms", "application/octet-stream");
     s_mapMimeTypes.put("doc", "application/msword");
     s_mapMimeTypes.put("dtd", "application/xml-dtd");
     s_mapMimeTypes.put("dvi", "application/x-dvi");
     s_mapMimeTypes.put("dxr", "application/x-director");
     s_mapMimeTypes.put("eps", "application/postscript");
     s_mapMimeTypes.put("etx", "text/x-setext");
     s_mapMimeTypes.put("exe", "application/octet-stream");
     s_mapMimeTypes.put("ez", "application/andrew-inset");
     s_mapMimeTypes.put("gif", "image/gif");
     s_mapMimeTypes.put("gtar", "application/x-gtar");
     s_mapMimeTypes.put("gz", "application/gzip");
     s_mapMimeTypes.put("gzip", "application/gzip");
     s_mapMimeTypes.put("hdf", "application/x-hdf");
     s_mapMimeTypes.put("htc", "text/x-component");
     s_mapMimeTypes.put("hqx", "application/mac-binhex40");
     s_mapMimeTypes.put("html", "text/html");
     s_mapMimeTypes.put("htm", "text/html");
     s_mapMimeTypes.put("ice", "x-conference/x-cooltalk");
     s_mapMimeTypes.put("ief", "image/ief");
     s_mapMimeTypes.put("iges", "model/iges");
     s_mapMimeTypes.put("igs", "model/iges");
     s_mapMimeTypes.put("jar", "application/java-archive");
     s_mapMimeTypes.put("java", "text/plain");
     s_mapMimeTypes.put("jnlp", "application/x-java-jnlp-file");
     s_mapMimeTypes.put("jpeg", "image/jpeg");
     s_mapMimeTypes.put("jpe", "image/jpeg");
     s_mapMimeTypes.put("jpg", "image/jpeg");
     s_mapMimeTypes.put("js", "application/x-javascript");
     s_mapMimeTypes.put("jsp", "text/plain");
     s_mapMimeTypes.put("kar", "audio/midi");
     s_mapMimeTypes.put("latex", "application/x-latex");
     s_mapMimeTypes.put("lha", "application/octet-stream");
     s_mapMimeTypes.put("lzh", "application/octet-stream");
     s_mapMimeTypes.put("man", "application/x-troff-man");
     s_mapMimeTypes.put("mathml", "application/mathml+xml");
     s_mapMimeTypes.put("me", "application/x-troff-me");
     s_mapMimeTypes.put("mesh", "model/mesh");
     s_mapMimeTypes.put("mid", "audio/midi");
     s_mapMimeTypes.put("midi", "audio/midi");
     s_mapMimeTypes.put("mif", "application/vnd.mif");
     s_mapMimeTypes.put("mol", "chemical/x-mdl-molfile");
     s_mapMimeTypes.put("movie", "video/x-sgi-movie");
     s_mapMimeTypes.put("mov", "video/quicktime");
     s_mapMimeTypes.put("mp2", "audio/mpeg");
     s_mapMimeTypes.put("mp3", "audio/mpeg");
     s_mapMimeTypes.put("mpeg", "video/mpeg");
     s_mapMimeTypes.put("mpe", "video/mpeg");
     s_mapMimeTypes.put("mpga", "audio/mpeg");
     s_mapMimeTypes.put("mpg", "video/mpeg");
     s_mapMimeTypes.put("ms", "application/x-troff-ms");
     s_mapMimeTypes.put("msh", "model/mesh");
     s_mapMimeTypes.put("msi", "application/octet-stream");
     s_mapMimeTypes.put("nc", "application/x-netcdf");
     s_mapMimeTypes.put("oda", "application/oda");
     s_mapMimeTypes.put("ogg", "application/ogg");
     s_mapMimeTypes.put("pbm", "image/x-portable-bitmap");
     s_mapMimeTypes.put("pdb", "chemical/x-pdb");
     s_mapMimeTypes.put("pdf", "application/pdf");
     s_mapMimeTypes.put("pgm", "image/x-portable-graymap");
     s_mapMimeTypes.put("pgn", "application/x-chess-pgn");
     s_mapMimeTypes.put("png", "image/png");
     s_mapMimeTypes.put("pnm", "image/x-portable-anymap");
     s_mapMimeTypes.put("ppm", "image/x-portable-pixmap");
     s_mapMimeTypes.put("ppt", "application/vnd.ms-powerpoint");
     s_mapMimeTypes.put("ps", "application/postscript");
     s_mapMimeTypes.put("qt", "video/quicktime");
     s_mapMimeTypes.put("ra", "audio/x-pn-realaudio");
     s_mapMimeTypes.put("ra", "audio/x-realaudio");
     s_mapMimeTypes.put("ram", "audio/x-pn-realaudio");
     s_mapMimeTypes.put("ras", "image/x-cmu-raster");
     s_mapMimeTypes.put("rdf", "application/rdf+xml");
     s_mapMimeTypes.put("rgb", "image/x-rgb");
     s_mapMimeTypes.put("rm", "audio/x-pn-realaudio");
     s_mapMimeTypes.put("roff", "application/x-troff");
     s_mapMimeTypes.put("rpm", "application/x-rpm");
     s_mapMimeTypes.put("rpm", "audio/x-pn-realaudio");
     s_mapMimeTypes.put("rtf", "application/rtf");
     s_mapMimeTypes.put("rtx", "text/richtext");
     s_mapMimeTypes.put("ser", "application/java-serialized-object");
     s_mapMimeTypes.put("sgml", "text/sgml");
     s_mapMimeTypes.put("sgm", "text/sgml");
     s_mapMimeTypes.put("sh", "application/x-sh");
     s_mapMimeTypes.put("shar", "application/x-shar");
     s_mapMimeTypes.put("silo", "model/mesh");
     s_mapMimeTypes.put("sit", "application/x-stuffit");
     s_mapMimeTypes.put("skd", "application/x-koan");
     s_mapMimeTypes.put("skm", "application/x-koan");
     s_mapMimeTypes.put("skp", "application/x-koan");
     s_mapMimeTypes.put("skt", "application/x-koan");
     s_mapMimeTypes.put("smi", "application/smil");
     s_mapMimeTypes.put("smil", "application/smil");
     s_mapMimeTypes.put("snd", "audio/basic");
     s_mapMimeTypes.put("spl", "application/x-futuresplash");
     s_mapMimeTypes.put("src", "application/x-wais-source");
     s_mapMimeTypes.put("sv4cpio", "application/x-sv4cpio");
     s_mapMimeTypes.put("sv4crc", "application/x-sv4crc");
     s_mapMimeTypes.put("svg", "image/svg+xml");
     s_mapMimeTypes.put("swf", "application/x-shockwave-flash");
     s_mapMimeTypes.put("t", "application/x-troff");
     s_mapMimeTypes.put("tar", "application/x-tar");
     s_mapMimeTypes.put("tar.gz", "application/x-gtar");
     s_mapMimeTypes.put("tcl", "application/x-tcl");
     s_mapMimeTypes.put("tex", "application/x-tex");
     s_mapMimeTypes.put("texi", "application/x-texinfo");
     s_mapMimeTypes.put("texinfo", "application/x-texinfo");
     s_mapMimeTypes.put("tgz", "application/x-gtar");
     s_mapMimeTypes.put("tiff", "image/tiff");
     s_mapMimeTypes.put("tif", "image/tiff");
     s_mapMimeTypes.put("tr", "application/x-troff");
     s_mapMimeTypes.put("tsv", "text/tab-separated-values");
     s_mapMimeTypes.put("txt", "text/plain");
     s_mapMimeTypes.put("ustar", "application/x-ustar");
     s_mapMimeTypes.put("vcd", "application/x-cdlink");
     s_mapMimeTypes.put("vrml", "model/vrml");
     s_mapMimeTypes.put("vxml", "application/voicexml+xml");
     s_mapMimeTypes.put("wav", "audio/x-wav");
     s_mapMimeTypes.put("wbmp", "image/vnd.wap.wbmp");
     s_mapMimeTypes.put("wmlc", "application/vnd.wap.wmlc");
     s_mapMimeTypes.put("wmlsc", "application/vnd.wap.wmlscriptc");
     s_mapMimeTypes.put("wmls", "text/vnd.wap.wmlscript");
     s_mapMimeTypes.put("wml", "text/vnd.wap.wml");
     s_mapMimeTypes.put("wrl", "model/vrml");
     s_mapMimeTypes.put("wtls-ca-certificate", "application/vnd.wap.wtls-ca-certificate");
     s_mapMimeTypes.put("xbm", "image/x-xbitmap");
     s_mapMimeTypes.put("xht", "application/xhtml+xml");
     s_mapMimeTypes.put("xhtml", "application/xhtml+xml");
     s_mapMimeTypes.put("xls", "application/vnd.ms-excel");
     s_mapMimeTypes.put("xml", "application/xml");
     s_mapMimeTypes.put("xpm", "image/x-xpixmap");
     s_mapMimeTypes.put("xpm", "image/x-xpixmap");
     s_mapMimeTypes.put("xsl", "application/xml");
     s_mapMimeTypes.put("xslt", "application/xslt+xml");
     s_mapMimeTypes.put("xul", "application/vnd.mozilla.xul+xml");
     s_mapMimeTypes.put("xwd", "image/x-xwindowdump");
     s_mapMimeTypes.put("xyz", "chemical/x-xyz");
     s_mapMimeTypes.put("z", "application/compress");
     s_mapMimeTypes.put("zip", "application/zip");
  }
  
  // Constructors /////////////////////////////////////////////////////////////
  
  /** 
   * Private constructor since this class cannot be instantiated
   */
  private MimeTypeConstants(
  )
  {
     // Do nothing
  }
  
  // Public methods ///////////////////////////////////////////////////////////
  
  /**
   * Method getting particular Mime type for the extension (key)
   * 
   * @param strKey - key value for returning Mime type
   * @return String
   */
  public static String getMimeType(
     String strKey
  )
  {
     String strMimeType = null;
     // get value for particular key
     strMimeType = s_mapMimeTypes.get(strKey).toString();
     if ((strMimeType == null) || (strMimeType.trim().length() == 0))
     {
        strMimeType = s_strDefaultMimeType;
     }
     
     return strMimeType;
  }

}

 </source>
   
  
 
  



Map file extensions to MIME types. Based on the Apache mime.types file

   <source lang="java">

// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.

import java.util.HashMap; /**

* Map file extensions to MIME types. Based on the Apache mime.types file.
* http://www.iana.org/assignments/media-types/
*/

public class MimeTypes {

 public static final String MIME_APPLICATION_ANDREW_INSET  = "application/andrew-inset";
 public static final String MIME_APPLICATION_JSON      = "application/json";
 public static final String MIME_APPLICATION_ZIP       = "application/zip";
 public static final String MIME_APPLICATION_X_GZIP      = "application/x-gzip";
 public static final String MIME_APPLICATION_TGZ       = "application/tgz";
 public static final String MIME_APPLICATION_MSWORD      = "application/msword";
 public static final String MIME_APPLICATION_POSTSCRIPT    = "application/postscript";
 public static final String MIME_APPLICATION_PDF       = "application/pdf";
 public static final String MIME_APPLICATION_JNLP      = "application/jnlp";
 public static final String MIME_APPLICATION_MAC_BINHEX40  = "application/mac-binhex40";
 public static final String MIME_APPLICATION_MAC_COMPACTPRO  = "application/mac-compactpro";
 public static final String MIME_APPLICATION_MATHML_XML    = "application/mathml+xml";
 public static final String MIME_APPLICATION_OCTET_STREAM  = "application/octet-stream";
 public static final String MIME_APPLICATION_ODA       = "application/oda";
 public static final String MIME_APPLICATION_RDF_XML     = "application/rdf+xml";
 public static final String MIME_APPLICATION_JAVA_ARCHIVE  = "application/java-archive";
 public static final String MIME_APPLICATION_RDF_SMIL    = "application/smil";
 public static final String MIME_APPLICATION_SRGS      = "application/srgs";
 public static final String MIME_APPLICATION_SRGS_XML    = "application/srgs+xml";
 public static final String MIME_APPLICATION_VND_MIF     = "application/vnd.mif";
 public static final String MIME_APPLICATION_VND_MSEXCEL   = "application/vnd.ms-excel";
 public static final String MIME_APPLICATION_VND_MSPOWERPOINT= "application/vnd.ms-powerpoint";
 public static final String MIME_APPLICATION_VND_RNREALMEDIA = "application/vnd.rn-realmedia";
 public static final String MIME_APPLICATION_X_BCPIO     = "application/x-bcpio";
 public static final String MIME_APPLICATION_X_CDLINK    = "application/x-cdlink";
 public static final String MIME_APPLICATION_X_CHESS_PGN   = "application/x-chess-pgn";
 public static final String MIME_APPLICATION_X_CPIO      = "application/x-cpio";
 public static final String MIME_APPLICATION_X_CSH     = "application/x-csh";
 public static final String MIME_APPLICATION_X_DIRECTOR    = "application/x-director";
 public static final String MIME_APPLICATION_X_DVI     = "application/x-dvi";
 public static final String MIME_APPLICATION_X_FUTURESPLASH  = "application/x-futuresplash";
 public static final String MIME_APPLICATION_X_GTAR      = "application/x-gtar";
 public static final String MIME_APPLICATION_X_HDF     = "application/x-hdf";
 public static final String MIME_APPLICATION_X_JAVASCRIPT  = "application/x-javascript";
 public static final String MIME_APPLICATION_X_KOAN      = "application/x-koan";
 public static final String MIME_APPLICATION_X_LATEX     = "application/x-latex";
 public static final String MIME_APPLICATION_X_NETCDF    = "application/x-netcdf";
 public static final String MIME_APPLICATION_X_OGG     = "application/x-ogg";
 public static final String MIME_APPLICATION_X_SH      = "application/x-sh";
 public static final String MIME_APPLICATION_X_SHAR      = "application/x-shar";
 public static final String MIME_APPLICATION_X_SHOCKWAVE_FLASH = "application/x-shockwave-flash";
 public static final String MIME_APPLICATION_X_STUFFIT     = "application/x-stuffit";
 public static final String MIME_APPLICATION_X_SV4CPIO     = "application/x-sv4cpio";
 public static final String MIME_APPLICATION_X_SV4CRC    = "application/x-sv4crc";
 public static final String MIME_APPLICATION_X_TAR       = "application/x-tar";
 public static final String MIME_APPLICATION_X_RAR_COMPRESSED= "application/x-rar-compressed";
 public static final String MIME_APPLICATION_X_TCL       = "application/x-tcl";
 public static final String MIME_APPLICATION_X_TEX       = "application/x-tex";
 public static final String MIME_APPLICATION_X_TEXINFO   = "application/x-texinfo";
 public static final String MIME_APPLICATION_X_TROFF     = "application/x-troff";
 public static final String MIME_APPLICATION_X_TROFF_MAN   = "application/x-troff-man";
 public static final String MIME_APPLICATION_X_TROFF_ME    = "application/x-troff-me";
 public static final String MIME_APPLICATION_X_TROFF_MS    = "application/x-troff-ms";
 public static final String MIME_APPLICATION_X_USTAR     = "application/x-ustar";
 public static final String MIME_APPLICATION_X_WAIS_SOURCE = "application/x-wais-source";
 public static final String MIME_APPLICATION_VND_MOZZILLA_XUL_XML = "application/vnd.mozilla.xul+xml";
 public static final String MIME_APPLICATION_XHTML_XML     = "application/xhtml+xml";
 public static final String MIME_APPLICATION_XSLT_XML    = "application/xslt+xml";
 public static final String MIME_APPLICATION_XML       = "application/xml";
 public static final String MIME_APPLICATION_XML_DTD     = "application/xml-dtd";
 public static final String MIME_IMAGE_BMP         = "image/bmp";
 public static final String MIME_IMAGE_CGM         = "image/cgm";
 public static final String MIME_IMAGE_GIF         = "image/gif";
 public static final String MIME_IMAGE_IEF         = "image/ief";
 public static final String MIME_IMAGE_JPEG          = "image/jpeg";
 public static final String MIME_IMAGE_TIFF          = "image/tiff";
 public static final String MIME_IMAGE_PNG         = "image/png";
 public static final String MIME_IMAGE_SVG_XML       = "image/svg+xml";
 public static final String MIME_IMAGE_VND_DJVU        = "image/vnd.djvu";
 public static final String MIME_IMAGE_WAP_WBMP        = "image/vnd.wap.wbmp";
 public static final String MIME_IMAGE_X_CMU_RASTER      = "image/x-cmu-raster";
 public static final String MIME_IMAGE_X_ICON        = "image/x-icon";
 public static final String MIME_IMAGE_X_PORTABLE_ANYMAP   = "image/x-portable-anymap";
 public static final String MIME_IMAGE_X_PORTABLE_BITMAP   = "image/x-portable-bitmap";
 public static final String MIME_IMAGE_X_PORTABLE_GRAYMAP  = "image/x-portable-graymap";
 public static final String MIME_IMAGE_X_PORTABLE_PIXMAP   = "image/x-portable-pixmap";
 public static final String MIME_IMAGE_X_RGB         = "image/x-rgb";
 public static final String MIME_AUDIO_BASIC         = "audio/basic";
 public static final String MIME_AUDIO_MIDI          = "audio/midi";
 public static final String MIME_AUDIO_MPEG          = "audio/mpeg";
 public static final String MIME_AUDIO_X_AIFF        = "audio/x-aiff";
 public static final String MIME_AUDIO_X_MPEGURL       = "audio/x-mpegurl";
 public static final String MIME_AUDIO_X_PN_REALAUDIO    = "audio/x-pn-realaudio";
 public static final String MIME_AUDIO_X_WAV         = "audio/x-wav";
 public static final String MIME_CHEMICAL_X_PDB        = "chemical/x-pdb";
 public static final String MIME_CHEMICAL_X_XYZ        = "chemical/x-xyz";
 public static final String MIME_MODEL_IGES          = "model/iges";
 public static final String MIME_MODEL_MESH          = "model/mesh";
 public static final String MIME_MODEL_VRLM          = "model/vrml";
 public static final String MIME_TEXT_PLAIN          = "text/plain";
 public static final String MIME_TEXT_RICHTEXT       = "text/richtext";
 public static final String MIME_TEXT_RTF          = "text/rtf";
 public static final String MIME_TEXT_HTML         = "text/html";
 public static final String MIME_TEXT_CALENDAR       = "text/calendar";
 public static final String MIME_TEXT_CSS          = "text/css";
 public static final String MIME_TEXT_SGML         = "text/sgml";
 public static final String MIME_TEXT_TAB_SEPARATED_VALUES = "text/tab-separated-values";
 public static final String MIME_TEXT_VND_WAP_XML      = "text/vnd.wap.wml";
 public static final String MIME_TEXT_VND_WAP_WMLSCRIPT    = "text/vnd.wap.wmlscript";
 public static final String MIME_TEXT_X_SETEXT       = "text/x-setext";
 public static final String MIME_TEXT_X_COMPONENT      = "text/x-component";
 public static final String MIME_VIDEO_QUICKTIME       = "video/quicktime";
 public static final String MIME_VIDEO_MPEG          = "video/mpeg";
 public static final String MIME_VIDEO_VND_MPEGURL     = "video/vnd.mpegurl";
 public static final String MIME_VIDEO_X_MSVIDEO       = "video/x-msvideo";
 public static final String MIME_VIDEO_X_MS_WMV        = "video/x-ms-wmv";
 public static final String MIME_VIDEO_X_SGI_MOVIE     = "video/x-sgi-movie";
 public static final String MIME_X_CONFERENCE_X_COOLTALK   = "x-conference/x-cooltalk";
 private static HashMap<String, String> mimeTypeMapping;
 static {
   mimeTypeMapping = new HashMap<String, String>(200) {
     private void put1(String key, String value) {
       if (put(key, value) != null) {
         throw new IllegalArgumentException("Duplicated extension: " + key);
       }
     }
     {
     put1("xul", MIME_APPLICATION_VND_MOZZILLA_XUL_XML);
     put1("json", MIME_APPLICATION_JSON);
     put1("ice", MIME_X_CONFERENCE_X_COOLTALK);
     put1("movie", MIME_VIDEO_X_SGI_MOVIE);
     put1("avi", MIME_VIDEO_X_MSVIDEO);
     put1("wmv", MIME_VIDEO_X_MS_WMV);
     put1("m4u", MIME_VIDEO_VND_MPEGURL);
     put1("mxu", MIME_VIDEO_VND_MPEGURL);
     put1("htc", MIME_TEXT_X_COMPONENT);
     put1("etx", MIME_TEXT_X_SETEXT);
     put1("wmls", MIME_TEXT_VND_WAP_WMLSCRIPT);
     put1("wml", MIME_TEXT_VND_WAP_XML);
     put1("tsv", MIME_TEXT_TAB_SEPARATED_VALUES);
     put1("sgm", MIME_TEXT_SGML);
     put1("sgml", MIME_TEXT_SGML);
     put1("css", MIME_TEXT_CSS);
     put1("ifb", MIME_TEXT_CALENDAR);
     put1("ics", MIME_TEXT_CALENDAR);
     put1("wrl", MIME_MODEL_VRLM);
     put1("vrlm", MIME_MODEL_VRLM);
     put1("silo", MIME_MODEL_MESH);
     put1("mesh", MIME_MODEL_MESH);
     put1("msh", MIME_MODEL_MESH);
     put1("iges", MIME_MODEL_IGES);
     put1("igs", MIME_MODEL_IGES);
     put1("rgb", MIME_IMAGE_X_RGB);
     put1("ppm", MIME_IMAGE_X_PORTABLE_PIXMAP);
     put1("pgm", MIME_IMAGE_X_PORTABLE_GRAYMAP);
     put1("pbm", MIME_IMAGE_X_PORTABLE_BITMAP);
     put1("pnm", MIME_IMAGE_X_PORTABLE_ANYMAP);
     put1("ico", MIME_IMAGE_X_ICON);
     put1("ras", MIME_IMAGE_X_CMU_RASTER);
     put1("wbmp", MIME_IMAGE_WAP_WBMP);
     put1("djv", MIME_IMAGE_VND_DJVU);
     put1("djvu", MIME_IMAGE_VND_DJVU);
     put1("svg", MIME_IMAGE_SVG_XML);
     put1("ief", MIME_IMAGE_IEF);
     put1("cgm", MIME_IMAGE_CGM);
     put1("bmp", MIME_IMAGE_BMP);
     put1("xyz", MIME_CHEMICAL_X_XYZ);
     put1("pdb", MIME_CHEMICAL_X_PDB);
     put1("ra", MIME_AUDIO_X_PN_REALAUDIO);
     put1("ram", MIME_AUDIO_X_PN_REALAUDIO);
     put1("m3u", MIME_AUDIO_X_MPEGURL);
     put1("aifc", MIME_AUDIO_X_AIFF);
     put1("aif", MIME_AUDIO_X_AIFF);
     put1("aiff", MIME_AUDIO_X_AIFF);
     put1("mp3", MIME_AUDIO_MPEG);
     put1("mp2", MIME_AUDIO_MPEG);
     put1("mp1", MIME_AUDIO_MPEG);
     put1("mpga", MIME_AUDIO_MPEG);
     put1("kar", MIME_AUDIO_MIDI);
     put1("mid", MIME_AUDIO_MIDI);
     put1("midi", MIME_AUDIO_MIDI);
     put1("dtd", MIME_APPLICATION_XML_DTD);
     put1("xsl", MIME_APPLICATION_XML);
     put1("xml", MIME_APPLICATION_XML);
     put1("xslt", MIME_APPLICATION_XSLT_XML);
     put1("xht", MIME_APPLICATION_XHTML_XML);
     put1("xhtml", MIME_APPLICATION_XHTML_XML);
     put1("src", MIME_APPLICATION_X_WAIS_SOURCE);
     put1("ustar", MIME_APPLICATION_X_USTAR);
     put1("ms", MIME_APPLICATION_X_TROFF_MS);
     put1("me", MIME_APPLICATION_X_TROFF_ME);
     put1("man", MIME_APPLICATION_X_TROFF_MAN);
     put1("roff", MIME_APPLICATION_X_TROFF);
     put1("tr", MIME_APPLICATION_X_TROFF);
     put1("t", MIME_APPLICATION_X_TROFF);
     put1("texi", MIME_APPLICATION_X_TEXINFO);
     put1("texinfo", MIME_APPLICATION_X_TEXINFO);
     put1("tex", MIME_APPLICATION_X_TEX);
     put1("tcl", MIME_APPLICATION_X_TCL);
     put1("sv4crc", MIME_APPLICATION_X_SV4CRC);
     put1("sv4cpio", MIME_APPLICATION_X_SV4CPIO);
     put1("sit", MIME_APPLICATION_X_STUFFIT);
     put1("swf", MIME_APPLICATION_X_SHOCKWAVE_FLASH);
     put1("shar", MIME_APPLICATION_X_SHAR);
     put1("sh", MIME_APPLICATION_X_SH);
     put1("cdf", MIME_APPLICATION_X_NETCDF);
     put1("nc", MIME_APPLICATION_X_NETCDF);
     put1("latex", MIME_APPLICATION_X_LATEX);
     put1("skm", MIME_APPLICATION_X_KOAN);
     put1("skt", MIME_APPLICATION_X_KOAN);
     put1("skd", MIME_APPLICATION_X_KOAN);
     put1("skp", MIME_APPLICATION_X_KOAN);
     put1("js", MIME_APPLICATION_X_JAVASCRIPT);
     put1("hdf", MIME_APPLICATION_X_HDF);
     put1("gtar", MIME_APPLICATION_X_GTAR);
     put1("spl", MIME_APPLICATION_X_FUTURESPLASH);
     put1("dvi", MIME_APPLICATION_X_DVI);
     put1("dxr", MIME_APPLICATION_X_DIRECTOR);
     put1("dir", MIME_APPLICATION_X_DIRECTOR);
     put1("dcr", MIME_APPLICATION_X_DIRECTOR);
     put1("csh", MIME_APPLICATION_X_CSH);
     put1("cpio", MIME_APPLICATION_X_CPIO);
     put1("pgn", MIME_APPLICATION_X_CHESS_PGN);
     put1("vcd", MIME_APPLICATION_X_CDLINK);
     put1("bcpio", MIME_APPLICATION_X_BCPIO);
     put1("rm", MIME_APPLICATION_VND_RNREALMEDIA);
     put1("ppt", MIME_APPLICATION_VND_MSPOWERPOINT);
     put1("mif", MIME_APPLICATION_VND_MIF);
     put1("grxml", MIME_APPLICATION_SRGS_XML);
     put1("gram", MIME_APPLICATION_SRGS);
     put1("smil", MIME_APPLICATION_RDF_SMIL);
     put1("smi", MIME_APPLICATION_RDF_SMIL);
     put1("rdf", MIME_APPLICATION_RDF_XML);
     put1("ogg", MIME_APPLICATION_X_OGG);
     put1("oda", MIME_APPLICATION_ODA);
     put1("dmg", MIME_APPLICATION_OCTET_STREAM);
     put1("lzh", MIME_APPLICATION_OCTET_STREAM);
     put1("so", MIME_APPLICATION_OCTET_STREAM);
     put1("lha", MIME_APPLICATION_OCTET_STREAM);
     put1("dms", MIME_APPLICATION_OCTET_STREAM);
     put1("bin", MIME_APPLICATION_OCTET_STREAM);
     put1("mathml", MIME_APPLICATION_MATHML_XML);
     put1("cpt", MIME_APPLICATION_MAC_COMPACTPRO);
     put1("hqx", MIME_APPLICATION_MAC_BINHEX40);
     put1("jnlp", MIME_APPLICATION_JNLP);
     put1("ez", MIME_APPLICATION_ANDREW_INSET);
     put1("txt", MIME_TEXT_PLAIN);
     put1("ini", MIME_TEXT_PLAIN);
     put1("c", MIME_TEXT_PLAIN);
     put1("h", MIME_TEXT_PLAIN);
     put1("cpp", MIME_TEXT_PLAIN);
     put1("cxx", MIME_TEXT_PLAIN);
     put1("cc", MIME_TEXT_PLAIN);
     put1("chh", MIME_TEXT_PLAIN);
     put1("java", MIME_TEXT_PLAIN);
     put1("csv", MIME_TEXT_PLAIN);
     put1("bat", MIME_TEXT_PLAIN);
     put1("cmd", MIME_TEXT_PLAIN);
     put1("asc", MIME_TEXT_PLAIN);
     put1("rtf", MIME_TEXT_RTF);
     put1("rtx", MIME_TEXT_RICHTEXT);
     put1("html", MIME_TEXT_HTML);
     put1("htm", MIME_TEXT_HTML);
     put1("zip", MIME_APPLICATION_ZIP);
     put1("rar", MIME_APPLICATION_X_RAR_COMPRESSED);
     put1("gzip", MIME_APPLICATION_X_GZIP);
     put1("gz", MIME_APPLICATION_X_GZIP);
     put1("tgz", MIME_APPLICATION_TGZ);
     put1("tar", MIME_APPLICATION_X_TAR);
     put1("gif", MIME_IMAGE_GIF);
     put1("jpeg", MIME_IMAGE_JPEG);
     put1("jpg", MIME_IMAGE_JPEG);
     put1("jpe", MIME_IMAGE_JPEG);
     put1("tiff", MIME_IMAGE_TIFF);
     put1("tif", MIME_IMAGE_TIFF);
     put1("png", MIME_IMAGE_PNG);
     put1("au", MIME_AUDIO_BASIC);
     put1("snd", MIME_AUDIO_BASIC);
     put1("wav", MIME_AUDIO_X_WAV);
     put1("mov", MIME_VIDEO_QUICKTIME);
     put1("qt", MIME_VIDEO_QUICKTIME);
     put1("mpeg", MIME_VIDEO_MPEG);
     put1("mpg", MIME_VIDEO_MPEG);
     put1("mpe", MIME_VIDEO_MPEG);
     put1("abs", MIME_VIDEO_MPEG);
     put1("doc", MIME_APPLICATION_MSWORD);
     put1("xls", MIME_APPLICATION_VND_MSEXCEL);
     put1("eps", MIME_APPLICATION_POSTSCRIPT);
     put1("ai", MIME_APPLICATION_POSTSCRIPT);
     put1("ps", MIME_APPLICATION_POSTSCRIPT);
     put1("pdf", MIME_APPLICATION_PDF);
     put1("exe", MIME_APPLICATION_OCTET_STREAM);
     put1("dll", MIME_APPLICATION_OCTET_STREAM);
     put1("class", MIME_APPLICATION_OCTET_STREAM);
     put1("jar", MIME_APPLICATION_JAVA_ARCHIVE);
   }};
 }
 public static void main(String[] args) {
   System.out.println(mimeTypeMapping.size());
 }
 /**
  * Registers MIME type for provided extension. Existing extension type will be overriden.
  */
 public static void registerMimeType(String ext, String mimeType) {
   mimeTypeMapping.put(ext, mimeType);
 }
 /**
  * Returns the corresponding MIME type to the given extension.
  * If no MIME type was found it returns "application/octet-stream" type.
  */
 public static String getMimeType(String ext) {
   String mimeType = lookupMimeType(ext);
   if (mimeType == null) {
     mimeType = MIME_APPLICATION_OCTET_STREAM;
   }
   return mimeType;
 }
 /**
  * Simply returns MIME type or null if no type is found.
  */
 public static String lookupMimeType(String ext) {
   return mimeTypeMapping.get(ext.toLowerCase());
 }

}

 </source>
   
  
 
  



MIME type mappings

   <source lang="java">

text/plain txt text text/html html htm text/xml xml text/css css text/rtf rtf application/rtf rtf text/vnd.wap.wml wml image/jpeg jpeg jpg jpe image/png png image/gif gif image/tiff tiff tif image/svg+xml svg svgz image/svg-xml svg application/pdf pdf model/vrml wrl application/smil smil text/javascript js application/x-javascript js text/vbscript vbs application/zip zip video/mpeg mpeg mpg mpe video/quicktime mov audio/midi mid audio/mpeg mp3 text/x-vcard vcf text/rdf rdf application/octet-stream

 </source>
   
  
 
  



MIME types to use for various requests

   <source lang="java">

/*

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/**

*
* <p>Utility class for declaring MIME types to use for various requests and provide
* utility manipulation methods.

*

Added Content-Encoding capability, with defaults * * @author * @version $Id: MimeType.java 516448 2007-03-09 16:25:47Z ate $ */ public class MimeType { public static final MimeType HTML = new MimeType("text/html", "UTF-8"); //FIXME: test public static final MimeType XHTML = new MimeType("text/xhtml"); public static final MimeType WML = new MimeType("text/vnd.wap.wml"); public static final MimeType XML = new MimeType("text/xml"); public static final MimeType VXML = new MimeType("text/vxml"); /** * Standard ContentType String, with no encoding appended. */ private String mimeType = ""; /** * null value means default encoding. * Otherwise, charset to be used. */ private String charSet = null; public MimeType(String mimeType) { if (mimeType == null) { throw new NullPointerException(); } this.mimeType = mimeType; } /** * */ public MimeType(String mimeType, String charSet) { if (mimeType == null) { throw new NullPointerException(); } this.mimeType = mimeType; this.charSet = charSet; } /** Extracts from this MimeType a user-friendly identifying code * ie "html" for "text/html" or "wml" for "text/vnd.wap.wml" * * @return the simplified type */ public String getCode() { String type = this.mimeType; // get everything after "/" type = type.substring(type.indexOf("/") + 1); // remove any dot in the name int idx = type.lastIndexOf("."); if (idx >= 0) { type = type.substring(idx + 1); } //remove anything before a "-" idx = type.lastIndexOf("-"); if (idx >= 0) { type = type.substring(idx + 1); } return type.toLowerCase(); } /** * Return the media type associated */ public String getContentType() { return this.mimeType; } /** * Return the character encoding associated, if any */ public String getCharSet() { return this.charSet; } /** * Convert this MimeType to its external String representation */ public String toString() { if (null == this.charSet) { return this.mimeType; } return this.mimeType + "; charset=" + this.charSet; } /** * Compare one MimeType to another */ public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof MimeType) { MimeType comp = (MimeType) obj; return this.toString().equals(comp.toString()); } else { return false; } } } </source>

Print MIME header

   <source lang="java">

import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainClass {

 public static void main(String args[]) {
   URL u;
   URLConnection uc;
   String header;
   try {
     u = new URL("http://www.jexp.ru");
     uc = u.openConnection();
     for (int j = 1;; j++) {
       header = uc.getHeaderField(j);
       if (header == null)
         break;
       System.out.println(uc.getHeaderFieldKey(j) + " " + header);
     }
   } catch (MalformedURLException e) {
     System.err.println("not a URL I understand.");
   } catch (IOException e) {
     System.err.println(e);
   }
 }

}


 </source>