ファイルのダウンロードをGroovyっぽく書きたい。
こくちーずから申込者のアイコンをダウンロードし、座席表用のアカウントつき画像を作成する座席表職人のためのGroovyスクリプトの画像をダウンロードする処理。
def url = new URL("http://img.tweetimag.es/i/orange_clover_n") def file = new File("orange_clover.png") url.withInputStream {input -> file.bytes = input.bytes }
もっと、Groovyっぽくしたい。(ファイルコピーのこんな感じ)
new File('dest.txt') << new File('src.txt').getBytes()
で、URLクラス見てたら、InputStream 返す openStream()ってのがあった。
public final InputStream openStream()
「Groovy URL openStream」ググったら、ghello の時のGroovyで声に出して読みたい Hello Worldがヒットした。
address = 'http://dl.dropbox.com/u/645223/ghello/HelloWorld.wav' file = new FileOutputStream(a.tokenize("/")[-1]); out = new BufferedOutputStream(file); out << new URL(address).openStream(); out.close(); java.awt.Desktop.desktop.open(new File('HelloWorld.wav'))
あぅ・・・前に同じ事試してた。。。
つうことは、これでいけるのか
new FileOutputStream("orange_clover.png") << new URL("http://img.tweetimag.es/i/orange_clover_n").openStream()
new BufferedOutputStream(new FileOutputStream("orange_clover2.png")) << new URL("http://img.tweetimag.es/i/orange_clover_n").openStream()
いけた。
- 添削
from [twitter:@nobeans] コメント参照。
new File("orange_clover2.png") << new URL("http://img.tweetimag.es/i/orange_clover_n").openStream()
File#leftShift(InputStream) ってそいういう意味だったのか(^_^;)
ありがとうございます!
from [twitter:@kazuchika] コメント参照
ant = new AntBuilder() ant.get(src:"http://img.tweetimag.es/i/orange_clover_n", dest:"orange_clover2.png")
Antでダウンロードもできるんだ。
巨大ファイルだったら、AntBuilderっと_〆(。。)
ありがとうございます!
添削歓迎
ここ間違ってるよ
こうした方がよくないか?
こういうことなんじゃないかな。
この環境だとこうなるよ
などなど
方法は、コメント、トラックバック、はてブ、Twitter @orange_clover宛 で、お願いします。