Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts

    Getting the Content Type

    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.

  • #2
    Regular Coder
    Join Date
    Jan 2008
    Location
    Willow Grove, PA
    Posts
    169
    Thanks
    1
    Thanked 27 Times in 27 Posts
    You could write a properties file and have the Properties class load that file.

    http://java.sun.com/j2se/1.4.2/docs/...roperties.html

    properties file:
    Code:
    pdf=application/pdf
    doc=application/msword
    code:
    Code:
    String contentType = props.getProperty(fileExrtension);

  • #3
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •