Java ImageIO.write() 最多需要 6 秒
我正在编写一个 Web 应用程序,需要将图像从 servlet 发送到客户端。图像是动态生成的并且相当大(+-2MB)。它可能是 jpeg、png 或 gif。
现在,我使用 ImageIO.write() 将图像写入输出流,但速度非常慢。客户端最多需要 6 秒才能看到图像。我需要加快速度。
有什么建议吗?
顺便提一句。我知道 寻找 ImageIO 的更快替代方案主题。但这对我没有帮助。 由于 PNG ImageMagick 速度很慢,因此不是解决方案 我测试过 JAI,结果更糟。
提前致谢
编辑:
向您展示一些代码:
BufferedImage bi = [code to generate Image];
response.setContentType(mime);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi,"png",out);
为了可读性,我剥离了异常处理。
I am writing an web application where I need to send an image from servlet to client. Image is generated dynamically and is quite big(+-2MB). It might be jpeg, png, or gif.
Now, I am using ImageIO.write() to write the image to output stream, but its veeeery slow. It takes up to 6 seconds till the client see the image. I need to speed it up.
Any suggestions?
btw. I am aware of Looking for a faster alternative to ImageIO topic. But it didn't help me.
Since it's slow with PNG ImageMagick is not a solution and
I have tested JAI and it was even worse.
Thanks in advance
Edit:
To show you some code:
BufferedImage bi = [code to generate Image];
response.setContentType(mime);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi,"png",out);
I stripped down Exception handling for readability.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,java 中的图像编码相当慢,但您可能还想确保安装了本机库,因为它们在性能上产生了相当明显的差异。
http://download.java.net/media /jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html
Image encoding in java is pretty slow in general but you may also want to ensure you have the native libraries installed as they make quite a noticeable difference in performance.
http://download.java.net/media/jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html
请注意,在创建 ImageInputStreams 和 图像输出流。可以通过调用 来关闭此功能ImageIO.setUseCache(false)。
有关更详细的说明,请参阅此答案。
Be aware that ImageIO by default uses temporary files as cache when creating ImageInputStreams and ImageOutputStreams. This can be switched off by calling ImageIO.setUseCache(false).
For a more detailed explanation see this answer.
您确定
需要这么长时间吗 - 也许还有另一个问题,例如
Are you sure that the
takes so long - maybe there is another problem, e.g.