public static void downloadImageToFile ( String url , Path file ) throws IOException { if ( Files . exists ( file . getParent ( ) ) ) { URL urlObject = new URL ( url ) ; URLConnection connection = urlObject . openConnection ( ) ; connection . setRequestProperty ( "User-Agent" , "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6" ) ; connection . setRequestProperty ( "Referer" , "http://www.google.com" ) ; FileUtils . copyInputStreamToFile ( connection . getInputStream ( ) , file . toFile ( ) ) ; } }
Downloads an image to a file with the adequate headers to the http query