Java ImageIO.write() 最多需要 6 秒 [英] Java ImageIO.write() takes up to 6 seconds

查看:63
本文介绍了Java ImageIO.write() 最多需要 6 秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Web 应用程序,我需要在其中将图像从 servlet 发送到客户端.图像是动态生成的,非常大(+-2MB).它可能是 jpeg、png 或 gif.

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.

现在,我使用 ImageIO.write() 将图像写入输出流,但它非常慢.客户端看到图像最多需要 6 秒.我需要加快速度.

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.

有什么建议吗?

顺便说一句.我知道寻找 ImageIO 的更快替代方案 主题.但这对我没有帮助.由于 PNG ImageMagick 很慢,因此它不是解决方案,并且我测试过 JAI,结果更糟.

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.

提前致谢

向您展示一些代码:

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.

推荐答案

Java 中的图像编码通常很慢,但您可能还需要确保安装了本机库,因为它们在性能上有显着差异.

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

这篇关于Java ImageIO.write() 最多需要 6 秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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