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

S

sygsix

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
 
R

Real Gagnon

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.
 
S

sygsix

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top