URLConnection を使って、指定されたURLの画像を並列で高速にダウンロードします。
コマンドラインで表示するだけなので、とりあえず画像の幅を表示しています。
ほんとは timeout もつけたいですね。
import java.awt.Image;
import java.net.*;
import java.io.*;
import javax.imageio.ImageIO;
public class Downloader extends Thread{
String urlStr;
int index;
Image[] imgs;
public Downloader(String urlStr, Image[] imgs, int index){
this.imgs = imgs;
this.urlStr = urlStr;
this.index = index;
}
public void run(){
try{
URL url = new URL(urlStr);
URLConnection urlc = url.openConnection();
imgs[index] = ImageIO.read(urlc.getInputStream());
} catch (IOException ie){
ie.printStackTrace();
}
}
public static void main(String[] args){
Image[] imgs = new Image[1];
new Downloader("http://funini.com/trip/imgs/denmark.jpg", imgs, 0).run();
System.out.println("Width = " + imgs[0].getWidth(null));
}
}
[an error occurred while processing this directive]