返回二进制流作为对请求 playframework 的响应
我在从播放框架返回二进制流时遇到问题。根据文档,如果框架发现控制器方法返回 Stream 或 File,则应自动返回二进制输出。好吧,我的方法返回 Stream[Byte] 但框架返回“Stream(100, ?)”而不是二进制流。
我做错了什么? 感谢您的回答, Tomas Herman
编辑
好吧,以防将来有人需要这个,看起来我以某种方式将它拼凑在一起。我只是从 Stream 构建一个字符串并返回它。它似乎工作正常,但我确信有一些更好的解决方案。
示例:
val builder = new StringBuilder()
builder.clear
stream foreach { x=> builder.append(x.toChar)}
val res = builder.toString
其中 res 是您的控制器方法应该返回的内容
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. Well my method returns a Stream[Byte] but the framework returns "Stream(100, ?)" rather then the binary stream.
What do i do wrong?
Thanks for the answers,
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. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能已经得到了您需要的东西。
Stream(100, ?)
是一个Stream
,其第一个值为100
,其余值尚未计算。要完整查看它,请尝试打印stream mkString ("Stream(", ", ", ")")
。You might have gotten what you need.
Stream(100, ?)
is aStream
whose first value is100
, and the remaining values have not yet been evaluated. To see it fully, try printingstream mkString ("Stream(", ", ", ")")
.