Presents your JAVA E-NEWSLETTER for September 30, 2002 <-------------------------------------------> DISCOVER MIMETYPE When returning content from a servlet or jsp page other than HTML, you need to know the Multipart Internet Mail Extension (MIME) type. Often you have to hard-code the information--but is there a better way? Although it doesn't support all the types you're likely to need, Java does provide automatic filename-to-MIME type mapping. The mapping is hidden within an interface called FileNameMap, which you can find in the java.net package. The map is obtained via the URLConnection class found in the same package. Once the map is obtained, getContentTypeFor is the only method available. By passing in a filename, the MIME type for that extension can be determined. Here's some simple example code: import java.net.*; public class FilenameMap { static public void main(String[] args) { FileNameMap map = URLConnection.getFileNameMap(); System.err.println(""+map.getContentTypeFor("blah.exe")); System.err.println(""+map.getContentTypeFor("blah.gif")); System.err.println(""+map.getContentTypeFor("blah.png")); System.err.println(""+map.getContentTypeFor("blah.jpeg")); System.err.println(""+map.getContentTypeFor("blah.doc")); System.err.println(""+map.getContentTypeFor("blah.xml")); System.err.println(""+map.getContentTypeFor("blah.html")); System.err.println(""+map.getContentTypeFor("blah.jsp")); System.err.println(""+map.getContentTypeFor("blah.tiff")); } } The code above returns the following values. Note that not all file extensions are known. application/octet-stream image/gif image/png image/jpeg null application/xml text/html null image/tiff ----------------------------------------