The code below allowed me to download all 575 MB and closes the FileOutputStream correctly:
package controllers
import play.api.mvc._
import play.api.Play.current
import play.api.libs.iteratee._
import play.api.libs.ws.WS._
import concurrent.ExecutionContext.Implicits.global
import java.io.FileOutputStream
object TestController extends Controller {
def example = Action {
val writer = new FileOutputStream("%s/tears_of_steel_1080p.mkv".format(System.getProperty("user.home")))
val asyncResult = Async {
Iteratee.foreach[Array[Byte]](chunk => writer write chunk)
} map { _ => Ok("OK") }
}
asyncResult.result onComplete {
case _ =>
println("Closing writer")
writer.close()
}
asyncResult
}
}
Again, this is on Play 2.1, so you'll have to substitute the onComplete for onRedeem. Also note the very long timeout set on the http request. WS.url defaults to a 2 minute timeout, which may be why the previous examples did not work. I got a timeout and could not finish downloading when I tried this code without the withTimeout method.