Skip to content

Instantly share code, notes, and snippets.

@jdelafon
Created July 5, 2016 08:31
Show Gist options
  • Save jdelafon/955f9cc809144158452695b29649caeb to your computer and use it in GitHub Desktop.
Save jdelafon/955f9cc809144158452695b29649caeb to your computer and use it in GitHub Desktop.
Streaming a binary file in Scala Play 2.4.5
import java.io.FileInputStream
import java.nio.file.Paths
import play.api.http.HttpEntity
import akka.stream.scaladsl.StreamConverters
import play.api.mvc._
import play.api.http.MimeTypes._
import play.api.http.HeaderNames._
import play.api.http.Status._
val path = Paths.get("asdf")
val file = path.toFile
val contentLength = file.length
val stream: FileInputStream = new FileInputStream(file)
val source = StreamConverters.fromInputStream(() => stream)
Result(
header = ResponseHeader(OK, Map(
CONTENT_TYPE -> BINARY,
CONTENT_LENGTH -> contentLength.toString,
CONNECTION -> "keep-alive"
)),
body = HttpEntity.Streamed(source, Some(contentLength), Some(BINARY))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment