Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying an image from url using java

Tags:

java

I am trying to download an image from URL. I use the Apache Commons library:

org.apache.commons.io.FileUtils
                .copyURLToFile(
                        new URL(
                                "https://lh3.ggpht.com/AXYMUV5cpne2vE9U3X8x87HgrbwijwiG50_yOnehU2MUPKDoJky-BKFOPZzZ07Pug8U=h230"),
                        new File("test.png"));

While opening the image it says not an PNG file. Is this a good way to download images from url to local?

Updated . Thankyou guys for your answers i undestand that i need to get the connection.getContentType(); and then save it as jpg or png as it is.

like image 554
Ramesh Avatar asked Apr 14 '26 04:04

Ramesh


2 Answers

if you check the url, It is JPEG file. Try to execute program after renaming file name to test.jpeg..

like image 163
Sagar Avatar answered Apr 16 '26 17:04

Sagar


Checking for the content type see this SO Question. Then use this to save the file to the right type.

like image 25
tsukimi Avatar answered Apr 16 '26 18:04

tsukimi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!