title: How to download image from from url in java?
body: <p>I have been trying to download an image from url via java.<br>
  I tried to solve it with many ways but I didn't succeed.</p>
  
  <p>I'm writing my ways hoping someone will find where it went wrong:</p>
  
  <p>With the first and second ways I get the following error while I'm trying to open the image:</p>
  
  <blockquote>
    <p>... file appears to be damaged, corrupted, or is too large </p>
  </blockquote>
  
  <p>...and its size is smaller than it should be.</p>
  
  <p>I guess it's related to encoding.</p>
  
  <p>First way:</p>
  
  <pre><code>URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
  InputStream in = url.openStream();
  Files.copy(in, Paths.get("someFile.jpg"), StandardCopyOption.REPLACE_EXISTING);
  in.close();
  </code></pre>
  
  <p>Second way:</p>
  
  <pre><code>File f= new File("c:\\image.jpg");
  URL myUrl = new URL("http://www.avajava.com/images/avajavalogo.jpg");
  FileUtils.copyURLToFile(myUrl, f);
  </code></pre>
  
  <p>With way 3 i get Exception in <code>thread "main" java.lang.IllegalArgumentException: image == null!</code></p>
  
  <p>Third way:</p>
  
  <pre><code>URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
  BufferedImage img = ImageIO.read(url);
  File file = new File("downloaded.jpg");
  ImageIO.write(img, "jpg", file);
  </code></pre>
  
  <p>I really need your help!!! I have been trying to solve it a long time ago without success.</p>
  
  <p>Thanks in advance!!!</p>