can any one write me the program to dowmload imges using java. i have wriitten a program using URL class it is able download index.html pages but not able to download images.

my source code:

import java.net.*;
import java.io.*;


public class url {

  public static void main(String[] args) {

      try {
        URL u = new URL("http://www.idlebrain.com/movie/photogallery/ashtachemma/index.html");

        InputStream is = u.openStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String theLine;
        while ((theLine = br.readLine()) != null) {
          System.out.println(theLine);
        }
      }
      catch (MalformedURLException ex) {
        System.err.println(ex);
      } 
      catch (IOException ex) {
        System.err.println(ex);      
      } 
      
    

  }

}

Instead of specifying an URI to a HTML page, specify a image URL, something like http://www.google.co.in/logos/closing_ceremonies.gif ; use a byte oriented stream for reading data instead of using a text oriented one like BufferedReader.

Though this approach leads to compact code, it's not really necessary since it performs a lot of voodoo like image encoding under the hood. Plus, you can be rest assured that the ImageIO approach will always be slower than pulling raw bytes over the wire and directly writing them to disk.

If performance is not a concern, the ImageIO approach seems good enough.

commented: true engough +1

true enough, but ImageIO will most likely lead to easier to read code, and is also very clear what is being done.

Instead of specifying an URI to a HTML page, specify a image URL, something like http://www.google.co.in/logos/closing_ceremonies.gif ; use a byte oriented stream for reading data instead of using a text oriented one like BufferedReader.

i don't understand how byte stream could read images. does it need help of applets or else.

i don't understand how byte stream could read images. does it need help of applets or else.

Images are comprised of bytes.
You want to read them.
Byte streams read bytes.
You use a byte stream.

...

commented: Spoon feed-- +3

i don't understand how byte stream could read images. does it need help of applets or else.

Since you seem to be thoroughly confused here, I will just post a minimalistic non-working snippet and allow you to fill the gaps. Anything more and I might be forced to give up my Moderator title for breaking the rules. :-)

try {
      URL url = new URL(SAMPLE_URL);
      // 'in' and 'out' declaration goes here
      try {
        byte buf[] = new byte[4 * 1024];  // 4kb buffer
        int read = -1;
        in = url.openStream();
        // create a new File 'f' which the output stream will write to.
        // Create it at a location of your choice with name as that of the image
        out = new FileOutputStream(f);
        while((read = in.read(buf)) != -1) {
          out.write(buf, 0, read);
        }
        out.flush();
      } finally {
        if(in != null)  in.close();
        if(out != null) out.close();
      }
    } catch(IOException ioe) {
      ioe.printStackTrace();
    }
commented: =P +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.