问题描述
我正在编写一个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);
出于可读性考虑,我删除了异常处理.
java中的图像编码通常很慢,但是您可能还需要确保已安装本机库,因为它们在性能上有很大的不同. /p>
http://download.java. net/media/jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html
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 andI 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.
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秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!