Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy download image from URL

I am wondering what the proper way to go about downloading the image from this RUL would be: http://www.hidemyass.com/proxy-list/img/port/7018246/1

The way I tried downloading it, leaves the file in an unknown format. Current code snippet I tested out is:

public void download(def address) {

    def file = new FileOutputStream(address.tokenize("/")[-1])
    def out = new BufferedOutputStream(file)
    out << new URL(address).openStream()
    out.close()
}
like image 621
StartingGroovy Avatar asked Jan 12 '11 22:01

StartingGroovy


1 Answers

Does this work? I believe it should:

public void download(def address) {
  new File("${address.tokenize('/')[-1]}.png").withOutputStream { out ->
    out << new URL(address).openStream()
  }
}
like image 94
tim_yates Avatar answered Sep 26 '22 11:09

tim_yates