How to download image from from url in java? -
i have been trying download image url via java.
tried solve many ways didn't succeed.
i'm writing ways hoping find went wrong:
with first , second ways following error while i'm trying open image:
... file appears damaged, corrupted, or large
...and size smaller should be.
i guess it's related 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 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 need help!!! have been trying solve long time ago without success.
thanks in advance!!!
you have specify full location 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);
its working fine me
Comments
Post a Comment