简体   繁体   English

返回二进制流作为对请求playframework的响应

[英]Return binary stream as response to request playframework

i have problem with returning binary stream from play framework. 我有从播放框架返回二进制流的问题。 According to documentation, the framework should automatically return binary output if it finds that the controller method returns either Stream or File. 根据文档,如果框架发现控制器方法返回Stream或File,则框架应自动返回二进制输出。 Well my method returns a Stream[Byte] but the framework returns "Stream(100, ?)" rather then the binary stream. 好吧,我的方法返回一个Stream [Byte],但框架返回“Stream(100,?)”而不是二进制流。

What do i do wrong? 我做错了什么? Thanks for the answers, Tomas Herman 谢谢你的答案,Tomas Herman


edit 编辑

ok in case anyone needs this in the future, it looks like i somehow hacked it together. 好的,如果有人在将来需要这个,看起来我不知何故一起黑了它。 I just build a string from the Stream and return that. 我只是从Stream构建一个字符串并返回它。 It seems to be working correctly but i'm sure there is some better solution. 它似乎工作正常,但我敢肯定有一些更好的解决方案。

example: 例:

val builder = new StringBuilder()
builder.clear
stream foreach { x=> builder.append(x.toChar)}
val res = builder.toString

where res is what your controller method is supposed to return 其中res是你的控制器方法应该返回的

You might have gotten what you need. 你可能已经得到了你需要的东西。 Stream(100, ?) is a Stream whose first value is 100 , and the remaining values have not yet been evaluated. Stream(100, ?)是一个Stream其第一个值为100 ,其余值尚未评估。 To see it fully, try printing stream mkString ("Stream(", ", ", ")") . 要完整地查看它,请尝试打印stream mkString ("Stream(", ", ", ")")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM