Type of image returned by a URL/CGI, and how to write it out?

W

Wolfgang

I'm getting images from a CGI server via a URL, two lines of code
below. The first line of code just sets the URL, the second line
creates the image.

URL url = new
URL("http://www.image.com:80/cgi-bin/gal/bild.sh?id=45751&size=16");

image =
java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);

QUESTIONS:
==========
1) How can I determine, programamatically, of what (MIME) type the
returned image is (I suspect it's GIF, BMP or JPEG, but don't know)

2) How do I write the image to a file, as the same type as it came (no
need to transform between types).

Thanks for your help.

Wolfgang,
Santa Barbara, CA
 
K

Knute Johnson

Wolfgang said:
I'm getting images from a CGI server via a URL, two lines of code
below. The first line of code just sets the URL, the second line
creates the image.

URL url = new
URL("http://www.image.com:80/cgi-bin/gal/bild.sh?id=45751&size=16");

image =
java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);

QUESTIONS:
==========
1) How can I determine, programamatically, of what (MIME) type the
returned image is (I suspect it's GIF, BMP or JPEG, but don't know)

You can't from the code you have and why would you care once you have
the image?
2) How do I write the image to a file, as the same type as it came (no
need to transform between types).

Why do you convert it to an image before you would write the file some
place? Just read the file with a stream and write it the same way. Use
the file name that came with it.
Thanks for your help.

Wolfgang,
Santa Barbara, CA

What are you really trying to do?
 
M

Marco Schmidt

Wolfgang:

[...]
1) How can I determine, programamatically, of what (MIME) type the
returned image is (I suspect it's GIF, BMP or JPEG, but don't know)
2) How do I write the image to a file, as the same type as it came (no
need to transform between types).

Just copy the bytes to a file - write every byte you receive from the
InputStream associated with a URLConnection to a FileOutputStream.

After that, run ImageInfo
<http://www.geocities.com/marcoschmidt.geo/image-info.html> on that
file to determine the file type. Then change the file extension using
File.rename.

You will have to do the mapping from ImageInfo.getFormat to an
appropriate file extension yourself, but that should be trivial.
Something like

public static String getFileExtension(ImageInfo ii) {
int format = ii.getFormat();
switch(format) {
case(ImageInfo.FORMAT_GIF): return "gif";
...
default: return null;
}
}

Regards,
Marco
 
W

Wolfgang

I never know a file name, as the URL looks like
http://www.image.com:80/cgi-bin/gal/bild.sh?id=45751&size=16

Basically I want to download all images from a remote database, and I
know the image number, which is passed as a parameter in the URL, more
or less goes from 1 to 100000 (like id=45751). The images are small
thumbnails, so this is fine in terms of disk volume.

If the file MIME type is always the same, than I can use your method,
except that I need to create a file name for each file, which is easy.
If the file type changes, then I need to use Marco's method (see
thread) . Also, I want all images to be of the same type, so I may
have to do conversion. Hopefully, though, all images are the same
type, so all I have to do is make up a name.

Thanks for your suggestion, and also thanks to Marco.

Wolfgang
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top