3

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.

Ramesh
  • 2,295
  • 5
  • 35
  • 64

3 Answers3

2

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

Sagar
  • 1,315
  • 8
  • 15
2

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

Community
  • 1
  • 1
tsukimi
  • 1,605
  • 2
  • 21
  • 36
0

Running wget on this URL and looking at the HTTP response, looks like the file type is explicitly specified as JPEG:

--2012-06-27 16:55:30--  https://lh3.ggpht.com/AXYMUV5cpne2vE9U3X8x87HgrbwijwiG50_yOnehU2MUPKDoJky-BKFOPZzZ07Pug8U=h230
Resolving lh3.ggpht.com... 74.125.237.108, 74.125.237.106, 74.125.237.107, ...
Connecting to lh3.ggpht.com|74.125.237.108|:443... connected.
HTTP request sent, awaiting response... 
  HTTP/1.0 200 OK
  Access-Control-Allow-Origin: *
  ETag: "v1"
  Expires: Thu, 28 Jun 2012 06:50:40 GMT
  Content-Disposition: inline;filename="unnamed.jpg"
  Content-Type: image/jpeg
  X-Content-Type-Options: nosniff
  Date: Wed, 27 Jun 2012 06:50:40 GMT
  Server: fife
  Content-Length: 46597
  X-XSS-Protection: 1; mode=block
  Cache-Control: public, max-age=86400, no-transform
  Age: 290
  Connection: Keep-Alive
Length: 46597 (46K) [image/jpeg]
n0rm1e
  • 3,796
  • 2
  • 28
  • 37