如何在播放框架作业中下载图像?

时间:2012-03-18 17:59:36

标签: java asynchronous concurrency playframework download

我有一个像这样的playframework Job类:

public class ImageDownloader extends Job {
 private String[] urls;
 private String dir;

 public ImageDownloader(){}
 public ImageDownloader(String[] urls,String dir){
   this.urls = urls;
   this.dir = dir;
 }

 @Override
 public void doJob() throws Exception {
   if(urls!=null && urls.length > 0){
     for (int i = 0; i < urls.length; i++) {
       String url = urls[i];
       //Dowloading
     }
   }
 }
}

Play(1.2.4)有许多令人惊叹的工具,可以让事情变得简单。

所以我想知道是否有办法让下载变得轻松漂亮?

1 个答案:

答案 0 :(得分:4)

built-in HTTP client in Play

有了它,你可以这样做HTTP请求:

HttpResponse res = WS.url("http://www.stackoverflow.com").get();

然后,您可以获取响应的内容,例如,流格式:

InputStream is = res.getStream();