5

I have been trying to download an image from url via java.
I tried to solve it with many ways but I didn't succeed.

I'm writing my ways hoping someone will find where it went wrong:

With the first and second ways I get the following error while I'm trying to open the image:

... file appears to be damaged, corrupted, or is too large

...and its size is smaller than it should be.

I guess it's related to encoding.

First way:

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

Second way:

File f= new File("c:\\image.jpg");
URL myUrl = new URL("http://www.avajava.com/images/avajavalogo.jpg");
FileUtils.copyURLToFile(myUrl, f);

With way 3 i get Exception in thread "main" java.lang.IllegalArgumentException: image == null!

Third way:

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

I really need your help!!! I have been trying to solve it a long time ago without success.

Thanks in advance!!!

azurefrog
  • 10,785
  • 7
  • 42
  • 56
coral
  • 181
  • 1
  • 1
  • 12
  • Here check [this](http://stackoverflow.com/a/5882039/5223322) out. This will help you – Ibrahim Ali Khan Sep 22 '16 at 14:22
  • 2
    The first way works on my side – Nicolas Filotto Sep 22 '16 at 14:22
  • Please attach your downloaded file from the 1st case here so we can verify if it's really damaged. Also I'd try downloading it via other means and doing diff on that and java-downloaded one to see what's the dfiference – Ivan Sep 22 '16 at 14:25
  • An image is only a binary content nothing more. Knowing that the first will work with any type of content (binary or not), it will work with images too – Nicolas Filotto Sep 22 '16 at 14:27
  • the only remark I could make is to use a try-with-resources statement to close your stream whatever happens, like this answer http://stackoverflow.com/a/32472138/1997376 – Nicolas Filotto Sep 22 '16 at 14:28
  • Possible duplicate of [How to download and save a file from Internet using Java?](https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java) – Robin Green Nov 17 '18 at 06:30

2 Answers2

10

You have to specify the full location for the destination file in way 3:

URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("D:\\image\\downloaded.jpg");
ImageIO.write(img, "jpg", file);
blacktide
  • 10,654
  • 8
  • 33
  • 53
Vadivelan
  • 101
  • 2
-1

When this happens usually the image you are trying to download is located inside a placeholder. Placeholders are used to give the image some nice black background in all browsers or any kind of valuable methods..

Try to open one of your downloaded images with small size with any text editor. You will probably be surprised to see an HTML code inside your supposed image. This HTML code will have a few lines, usually a background color tag and the image real URL. This is the URL that you have to include in your code in order to download the full image.

This could be an example of the avajavalogo.jpg file that you are actually downloading:

<!DOCTYPE html>
<html>
<body style="background-color:#0e0e0e;">
<center><img src="http://www.myhostsiteisgreat.com/image_real_location_url/awesome_image.jpg" border="0" onclick="Javascript: window.close();" alt="Click on image to close it."></center>
</body>
</html>

If you try to open this file with an image editor, it will throw an error (wrong headers, bad format, etc) and the image will not be opened. It is not the case of the image you have showed in your question, but it could be the case of the images you are actually trying to download.

Apart from that, the methods exposed are valid.

fenix121
  • 1
  • 1