title: Java - Download image from URL
body: <p>Im trying to download an image from the following url in java:
  <a href="http://placehold.it/600/24f355" rel="nofollow noreferrer">http://placehold.it/600/24f355</a></p>
  
  <p>if you follow the url above you will see a placeholder image.</p>
  
  <p>the url that official holds the image is <a href="https://placeholdit.imgix.net/~text?txtsize=56&amp;bg=24f355&amp;txt=600%C3%97600&amp;w=600&amp;h=600" rel="nofollow noreferrer">https://placeholdit.imgix.net/~text?txtsize=56&amp;bg=24f355&amp;txt=600%C3%97600&amp;w=600&amp;h=600</a></p>
  
  <p><strong>Note</strong>: Please note that i only have direct access to the first URL. The final url i have no way to know apart from following the first url manually through a browser</p>
  
  <p>I've tried multiple ways to download this image but none of them were successfull.</p>
  
  <p>The code i have at the moment that is expected to download the image is the following:</p>
  
  <p>(The code downloads a image that cannot be opened..)</p>
  
  <pre><code>     public void saveImage(String imageUrl, String path) {
          // This method only saves the "dummy" image
          try{
              URL url = new URL(imageUrl);
              InputStream is = url.openStream();
              OutputStream os = new FileOutputStream(path);
  
              byte[] b = new byte[2048];
              int length;
  
              while ((length = is.read(b)) != -1) {
                  os.write(b, 0, length);
              }
  
              is.close();
              os.close();
          }catch(IOException e){
              e.printStackTrace();
          }
  
      }   
  }
  </code></pre>
  
  <p>the function receives the image url which is the one i refered at the top and specifies a path on the system where the image will be saved.</p>
  
  <p>What am i doing wrong? </p>
  
  <p>Your help would be awesome!
  Thanks in advance</p>