...

View Full Version : Getting the Content Type



brad211987
02-01-2008, 09:38 PM
Does anyone know of any utilities that can give you the http content type of a file based on its extension? I couldn't find anything in the javadocs and google hasn't been very much help for me so far.

What I'm looking to do is take a file, for example a pdf file: myfile.pdf. Look at the extension and know to set the content type on the http response to "application/pdf" or "application/msword" for word docs etc....

I can do this, but I cannot find a way to do it dynamically without writing an individual case for any document type I want to support. That just seems like an ugly way to handle it.

sobrien79
02-01-2008, 11:09 PM
You could write a properties file and have the Properties class load that file.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html

properties file:


pdf=application/pdf
doc=application/msword


code:


String contentType = props.getProperty(fileExrtension);

brad211987
02-02-2008, 02:38 PM
That is the direction I was leaning towards, but I was hoping to get out of it. This is a really simple program and was hoping to leave out any complexity that I could. Thanks for the reply, that solution will work great.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum