Is it possible to declare multiple MIME (content) types?

I am writing a servlet that is outputting JPEG image data, but some of the images only work if I set the contentType to image/pjpeg. Is there a way to set multiple content types, so that if the image works with image/jpeg it will load, but if not it will use image/pjpeg? There's no way for me to tell which format the image data will be in unless I read the first few bytes of the data (which I'd like to avoid).

Is this valid syntax?

response.setContentType("image/jpeg, image/pjpeg");

Thanks,

AC

Message was edited by:

apc123

[599 byte] By [apc123a] at [2007-11-27 3:36:46]
# 1
No. You have to set the content type.
DrClapa at 2007-7-12 8:40:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
I know that I have to set the content type, but does it have to be a single value in the setContentType method? Is there a way to say "if this format doesn't work, try this one"?
apc123a at 2007-7-12 8:40:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You can use String contentType = URLConnection.guessContentTypeFromName(fileNameWithExtension);

to retrieve the actual contenttype. For JPEG files this will be "image/jpeg" and voor JFIF files this will be "image/pipeg".

Also see http://balusc.xs4all.nl/srv/dev-jep-img.html for an ImageServlet example which uses the above construction.

BalusCa at 2007-7-12 8:40:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thanks for the reply. I didn't know that method existed.

I'm actually pulling the raw image data from a database and don't store the file name, but I can use the method that takes an InputStream:

byte[] img;

// get the image data from the db...

InputStream is = new ByteArrayInputStream(img);

String contentType = URLConnection.guessContentTypeFromStrea(is);

apc123a at 2007-7-12 8:40:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
This is expensive if the image is relatively big. I recommend to store original filenames (or at least the extensions) in the DB too.
BalusCa at 2007-7-12 8:40:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...