thumbnailator报错信息
找到问题根源就好说了,去到thumbnailator的官方GitHub查阅了一番,
thumbnailator库对图片的要求其实是极其严苛的,对图片的处理会消耗大量的内存,Thumbnailator需要加载原始图像,大致需要至少两倍的宽度*高度* 4个字节的堆空间。例如,一个20M像素的图像需要160 MB的堆。这个数字计算出来相当惊人了,一般对于JVM的内存会设置在1G左右,然而单次请求一个图片裁剪接口就占用了大量的内存!!!
说了这么多都是在说原因,解决这个问题需要多方配合:1、调整JVM的内存大小,2、可以对原图片做限制,大于多少像素提示用户图片不符合。或者你也可以使用其他的第三方库,例如:ImageMagick
最后总结,其主要错误是内存问题,报错为如下:
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.(DataBufferInt.java:75) ~[na:1.8.0_202]
at java.awt.image.Raster.createPackedRaster(Raster.java:467) ~[na:1.8.0_202]
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) ~[na:1.8.0_202]
at java.awt.image.BufferedImage.(BufferedImage.java:333) ~[na:1.8.0_202]
at sun.java2d.loops.GraphicsPrimitive.convertFrom(GraphicsPrimitive.java:557) ~[na:1.8.0_202]
at sun.java2d.loops.GraphicsPrimitive.convertFrom(GraphicsPrimitive.java:541) ~[na:1.8.0_202]
at sun.java2d.loops.MaskBlit$General.MaskBlit(MaskBlit.java:189) ~[na:1.8.0_202]
at sun.java2d.loops.Blit$GeneralMaskBlit.Blit(Blit.java:204) ~[na:1.8.0_202]
at sun.java2d.pipe.DrawImage.blitSurfaceData(DrawImage.java:959) ~[na:1.8.0_202]
at sun.java2d.pipe.DrawImage.renderImageCopy(DrawImage.java:577) ~[na:1.8.0_202]
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:67) ~[na:1.8.0_202]
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1014) ~[na:1.8.0_202]
at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:186) ~[na:1.8.0_202]
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3318) ~[na:1.8.0_202]
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3296) ~[na:1.8.0_202]
at net.coobird.thumbnailator.filters.Rotation$1.apply(Unknown Source) ~[thumbnailator-0.4.8.jar!/:0.4.8]
at net.coobird.thumbnailator.Thumbnailator.createThumbnail(Unknown Source) ~[thumbnailator-0.4.8.jar!/:0.4.8]
at net.coobird.thumbnailator.Thumbnails$Builder.toOutputStream(Unknown Source) ~[thumbnailator-0.4.8.jar!/:0.4.8]
at io.choerodon.file.infra.utils.ImageUtils.cutImage(ImageUtils.java:21) ~[classes!/:2021.3.18-174417-master-774]
at io.choerodon.file.app.service.impl.FileServiceImpl.cutImage(FileServiceImpl.java:86) ~[classes!/:2021.3.18-174417-master-774]
at io.choerodon.file.api.controller.FileController.cutImage(FileController.java:90) ~[classes!/:2021.3.18-174417-master-774]
至此结束,如有错误,欢迎大家指正!!!!!