如何使用Channels和Scala并行下载文件?

时间:2015-11-24 07:37:26

标签: java scala download channel

如何使这段代码并行下载文件?

object Files{
  def main(args: Array[String]): Unit ={

    System.setProperty("http.proxyHost", "abc");
    System.setProperty("http.proxyPort", "8080");

    var num = "000"
    for(x <- 1 to 999){
      if(x < 10){
        num = ("00" + x).toString
      }else if(x < 100 && x > 9){
        num = ("0" + x).toString
      }else{
        num = x.toString
      }
      println(num)
      var  myUrl = new URL("http://www.example.com/1" + num + ".jpg");
      var myUrlStream = myUrl.openStream();
      var myUrlChannel = Channels.newChannel(myUrlStream);

      var destinationChannel = new FileOutputStream("C:\\10"\\ + num + ".jpg").getChannel();
      try{
        destinationChannel.transferFrom(myUrlChannel, 0, Integer.MAX_VALUE);
      }catch{
        case ioe: IOException => ioe.printStackTrace()
      }
    }
  }
}

1 个答案:

答案 0 :(得分:3)

您可以使用并行集合(请注意par调用):

(1 to 999).par.foreach { x =
  if(x < 10){
    num = ("00" + x).toString
  }else if(x < 100 && x > 9){
    num = ("0" + x).toString
  }else{
    num = x.toString
  }
  println(num)
  var  myUrl = new URL("http://www.example.com/1" + num + ".jpg");
  var myUrlStream = myUrl.openStream();
  var myUrlChannel = Channels.newChannel(myUrlStream);

  var destinationChannel = new FileOutputStream("C:\\10"\\ + num + ".jpg").getChannel();
  try{
    destinationChannel.transferFrom(myUrlChannel, 0, Integer.MAX_VALUE);
  }catch{
    case ioe: IOException => ioe.printStackTrace()
  }
}