How to know the response content type of an uploaded binary file?

Discussion in 'Java' started by sygsix@gmail.com, Feb 7, 2005.

  1. Guest

    I am trying to create links to file that have been uploaded and am
    having problems. Up until now I've always known the content type, and
    as such have always put a
    response.setContentType("application/x-msword"), for example, in my
    class.

    But now the user can upload x files, of any type. So my question is:

    Is there any way to get the content type from the binary data stored in
    my database, to be able to set the response type accordingly? Or do I
    have to save that info somehow in my database as well? (A column for
    "content_type", for example ...)

    Many thanks in advance,
    syg
     
    , Feb 7, 2005
    #1
    1. Advertising

  2. Real Gagnon Guest

    > Is there any way to get the content type from the binary data stored in
    > my database, to be able to set the response type accordingly? Or do I
    > have to save that info somehow in my database as well? (A column for
    > "content_type", for example ...)


    You have two ways to guess the MIME type.
    By examining the file extension or looking for a "magic header".

    See http://www.rgagnon.com/javadetails/java-0487.html

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Looking for Java or PB snippets ? Visit Real's How-to
    * http://www.rgagnon.com/howto.html
     
    Real Gagnon, Feb 7, 2005
    #2
    1. Advertising

  3. Guest

    Excellent! There was one problem with the example on this page. Instead
    of instantiating a new MagicParser() you must instantiate a Magic().
    Then you can call getMagicMatch(file) or getMagicMatch(byte[]).
    (getMagicMatch() doesn't exist for MagicParser)

    It seems to do the trick, thanks for the tip!

    syg

    Real Gagnon wrote:
    > > Is there any way to get the content type from the binary data

    stored in
    > > my database, to be able to set the response type accordingly? Or do

    I
    > > have to save that info somehow in my database as well? (A column

    for
    > > "content_type", for example ...)

    >
    > You have two ways to guess the MIME type.
    > By examining the file extension or looking for a "magic header".
    >
    > See http://www.rgagnon.com/javadetails/java-0487.html
    >
    > Bye.
    > --
    > Real Gagnon from Quebec, Canada
    > * Looking for Java or PB snippets ? Visit Real's How-to
    > * http://www.rgagnon.com/howto.html
     
    , Feb 8, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt G
    Replies:
    1
    Views:
    1,199
    Deepak Kumar Vasudevan
    Aug 22, 2003
  2. Spotted Owl Eater

    Determine the File Type of an Uploaded File

    Spotted Owl Eater, Nov 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,871
    Patrice
    Nov 16, 2005
  3. Replies:
    2
    Views:
    345
    Christian Tismer
    Feb 23, 2004
  4. Mark
    Replies:
    4
    Views:
    803
  5. Roberto
    Replies:
    3
    Views:
    331
Loading...

Share This Page