How to get MIME type from byte[]

HI! I call a web service which returns me a byte[] (file), but I don't know how to get the MIME type from this byte[]?Any help?Thanks.
[156 byte] By [k@tzea] at [2007-10-3 3:02:01]
# 1
It should be in the headers. If not, you're doomed.If you open an HttpURLConnection you should be able to dig out a "Content-type" header.
malcolmmca at 2007-7-14 20:51:45 > top of Java-index,Java Essentials,Java Programming...
# 2
But all I have is the byte[] which contains the file...
k@tzea at 2007-7-14 20:51:45 > top of Java-index,Java Essentials,Java Programming...
# 3

> But all I have is the byte[] which contains the

> file...

Then you're doomed.

But how are you getting the data?

If it's a web service then what's stopping you from retrieving it in a way that does show you the headers?

Just sticking the URL into a web browser would, more likely than not, tell you what you need to know.

malcolmmca at 2007-7-14 20:51:45 > top of Java-index,Java Essentials,Java Programming...
# 4

Well, you're not totally doomed, but it would be much better if the webservice you called would return the proper mime-type.

You may be able to use something like the "file" command on Linux to determine the mime-type based on the contents. The [url=http://aperture.sourceforge.net/]Aperture project[/url] has an implementation of that functionality in Java: http://aperture.sourceforge.net/doc/javadoc/org/semanticdesktop/aperture/mime/identifier/package-summary.html

Herko_ter_Horsta at 2007-7-14 20:51:45 > top of Java-index,Java Essentials,Java Programming...
# 5
Another idea is to actually look at the data, in hex and try and show it as characters.
malcolmmca at 2007-7-14 20:51:45 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks Herko_ter_Horst, that's exactly what I want, but Sun doesn't provider these API? How come? It's a shame for Sun that we have to get a third party API to do a simple job...Anyway
k@tzea at 2007-7-14 20:51:45 > top of Java-index,Java Essentials,Java Programming...
# 7

Sun doesn't provide every API under the eh... sun, thank goodness. The standard APIs are already extremely rich as they are.

Getting the type of content from the content itself is a very specific, non-trivial problem that has no 100% solution. There is no reason why this type of functionality should be in the standard API of a general purpose Java Runtime Environment.

Herko_ter_Horsta at 2007-7-14 20:51:45 > top of Java-index,Java Essentials,Java Programming...