Created
July 5, 2016 08:31
-
-
Save jdelafon/955f9cc809144158452695b29649caeb to your computer and use it in GitHub Desktop.
Streaming a binary file in Scala Play 2.4.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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