0

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);      
      } 
      
    

  }

}
4
Contributors
7
Replies
9
Views
8 Years
Discussion Span
Last Post by ~s.o.s~
Featured Replies
  • 1
    ~s.o.s~ 2,560   8 Years Ago

    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 … Read More

  • 1
    Ezzaral 2,714   8 Years Ago

    [QUOTE=ravikiran032;677274]i don't understand how byte stream could read images. does it need help of applets or else.[/QUOTE] Images are comprised of bytes. You want to read them. Byte streams read bytes. You use a byte stream. ... Read More

  • 1
    ~s.o.s~ 2,560   8 Years Ago

    [QUOTE=ravikiran032;677274]i don't understand how byte stream could read images. does it need help of applets or else.[/QUOTE] 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 … Read More

1

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.

Comments
true engough
0

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

1

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.

...

Comments
Spoon feed--
1

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();
    }
Comments
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.