简体   繁体   English

Java ImageIO.write()最多需要6秒

[英]Java ImageIO.write() takes up to 6 seconds

I am writing an web application where I need to send an image from servlet to client. 我正在编写一个Web应用程序,我需要将图像从servlet发送到客户端。 Image is generated dynamically and is quite big(+-2MB). 图像是动态生成的,并且很大(+ -2MB)。 It might be jpeg, png, or gif. 可能是jpeg,png或gif。

Now, I am using ImageIO.write() to write the image to output stream, but its veeeery slow. 现在,我正在使用ImageIO.write()将图像写入输出流,但是其速度很慢。 It takes up to 6 seconds till the client see the image. 客户最多需要6秒钟才能看到图像。 I need to speed it up. 我需要加快速度。

Any suggestions? 有什么建议么?

btw. 顺便说一句。 I am aware of Looking for a faster alternative to ImageIO topic. 我知道正在寻找ImageIO主题的更快替代方案 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. 由于PNG运行缓慢,因此ImageMagick不是解决方案,我已经测试了JAI,甚至更糟。

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. 通常,Java中的图像编码速度非常慢,但是您可能还需要确保已安装本机库,因为它们在性能上有很大的不同。

http://download.java.net/media/jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html http://download.java.net/media/jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html

Be aware that ImageIO by default uses temporary files as cache when creating ImageInputStreams and ImageOutputStreams . 请注意,默认情况下,ImageIO在创建ImageInputStreamsImageOutputStreams时将临时文件用作缓存。 This can be switched off by calling ImageIO.setUseCache(false) . 可以通过调用ImageIO.setUseCache(false)将其关闭。

For a more detailed explanation see this answer . 有关更详细的说明,请参见此答案

Are you sure that the 您确定

ImageIO

takes so long - maybe there is another problem, eg 需要很长时间-也许还有另一个问题,例如

  • slow (network) connection to the client 与客户端的慢速(网络)连接
  • the generation (calculation) of the image takes a lot of time 图像的生成(计算)需要很多时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM