返回1 * 1像素图像响应播放scala [英] return 1*1 pixel image in response play scala

查看:107
本文介绍了返回1 * 1像素图像响应播放scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每个响应中返回1 * 1像素的相同虚拟图像。我使用bufferArray来执行此操作,我的代码snipet如下:

i need to return a same dummy image of 1*1 pixel in every response. I am using bufferArray to do this, my code snipet is as follow :

val image: BufferedImage = ImageIO.read(new File("public/images/dummy.png"));
val baos: ByteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);

Ok(baos.toByteArray).as("image/png")



<经过一段时间服务器抛出错误后,java.io.excp打开了太多文件。
请帮助,有没有其他方法可以做到这一点?

after some time server throws error, java.io.excp too many files open. please help, is there any another way to do this ?

我已将该imagebuffer部分放入另一个对象(即对象abc {})并将其用作abc.baos。但错误是一样的。

i have put that imagebuffer part in another object (i.e. object abc{}) and using it as abc.baos. but the error is same.

推荐答案

您应该能够将该文件作为响应提供。 Play有文件的响应编写器,因此不需要ByteArrayBuffer。如果您这样做,Play也应该在服务后为您关闭该文件。

You should be able to just serve the file as a response. Play has a responsewriter for files, so no need for the ByteArrayBuffer. If you do so Play should also close the file for you after it was being served.

同时在主帖下检查我对缓存的评论

Also checkout my comment on caching under your main post

这篇关于返回1 * 1像素图像响应播放scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆