Guess the Content-type from Filename

Java has a helpful little function for guessing the content-type based on the file name:

URLConnection.getFileNameMap().getContentTypeFor(filename)

You can check out — or customize — the mappings which are stored in $JRE_HOME/lib/content-types.properties.

So if you’re serving up a file and you’re not quite sure what content-type to use, let Java take a guess.

Comments are closed.