: I wrote a program that downloads a web page. I need to know how to download the images associated with the web page. Given an absolute address like http://www.yahoo.com/try.img, the program should copy try.img to C:\try.img. Source code would be a big help because I am new to java.
: Thank you,
Assuming the page's source is XML compliant, you can use Java's API for XML parsing. Through the generated Document, you could access the img elements with the src attributes and get local and occationally absolute references to the images.
Parsing of an XML file: http://kickjava.com/2515.htm
If you can't assume the page is XML compliant, you will have to find your own way of accessing image references. Maybe you could search the whole page's source for substrings like "src=" and work from there.
Now you have a url for an image to download. You could create a URL object to wrap around that String. This URL object can be used a couple different ways. You could either load the image into an Image object or directly save the downloaded data into a file.
To load the image into an image object, make a call to the getImage method of the Toolkit class.
Toolkit's getImage method: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#getImage(java.net.URL)
As long as the image is in a supported method, that would work. jpg, png, gif are all supported.
For writing the image to a file:
To write the data directly to a file, open a FileOutputStream so you can read from the URL's openStream() and immediately write all the data into your file.
URL's openStream method: http://java.sun.com/j2se/1.3/docs/api/java/net/URL.html#openStream()