title: How to download embedded images from websites Java
body: <p>I am trying to download <strong>the first 20</strong> images/comics from xkcd website. 
  The code I've written allows me to download a text file of the website or image if I change the fileName to "xkcd.jpg" and the URL to "<a href="http://imgs.xkcd.com/comics/monty_python.jpg" rel="nofollow noreferrer">http://imgs.xkcd.com/comics/monty_python.jpg</a>"</p>
  
  <p>The problem is that I need to download the embedded image on the site, without having to go back and forth copying the <strong>Image URLS</strong> of each comic over and over, that defeats the purpose of this program. I am guessing I need a for-loop at some point but I can't do that if I don't know how to download the embedded image on the website itself.
  I hope my explanation isn't too complicated</p>
  
  <p>Below is my code</p>
  
  <hr>
  
  <pre><code>String fileName = "xkcd.txt";
  URL url = new URL("http://xkcd.com/16/");
  InputStream in = new BufferedInputStream(url.openStream());
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  byte[] buf = new byte[1024];
  int n = 0;
  while (-1 != (n = in.read(buf))) {
      out.write(buf, 0, n);
  }
  out.close();
  in.close();
  byte[] response = out.toByteArray();
  FileOutputStream fos = new FileOutputStream(fileName);
  fos.write(response);
  fos.close();
  </code></pre>