Downloading Resources - pford68/groovy-examples GitHub Wiki

Examples

In short, you open a stream from a URL, using URL.openStream(), then send the output to a local File object.

Downloading A File

def url = 'http://www.google.com/images/logo.gif'  
        def file = new File('google_logo.gif').newOutputStream()  
        file << new URL(url).openStream()  
        file.close() 
URL penny = new URL('http://tankian99.files.wordpress.com/2011/08/kaley_cuoco_by_m4rios.jpg')
new File('/tmp/penny.jpg') << penny.openStream()

Downloading the Kibana distribution:

URL resource = new URL('https://artifacts.elastic.co/downloads/kibana/kibana-5.3.1-darwin-x86_64.tar.gz')
new File('/Users/philip/Downloads/test/kibana.tar.gz') << resource.openStream()

Saving Web Page Content To A File

new File("output.xml") << new URL ("http://some.url/some/path.xml").getText()

Related