java.io.EOFException: Unexpected end of ZLIB input stream解决

xiaoxiao2021-02-28  105

EOFException 表示异常文件结尾,说明读取的数据是不完整的。我在线上就遇到过类似的问题,在请求高峰时期就抛出了这个异常。下面是我的代码:

FileOutputStream fileOutputStream = null; try { //得到文件 File file = new File(fileName); fileOutputStream = new FileOutputStream(file); IOUtils.write(dataByte, fileOutputStream); // 在这里及时的close掉fileOutputStream //读取图片 BufferedImage bi = ImageIO.read(file); pictureProcessVo.setWidth(bi.getWidth()); //获得 宽度 pictureProcessVo.setHeight(bi.getHeight()); //获得 高度 // other operation // delete file FileUtils.deleteQuietly(file); } catch (Exception e) { LOGGER.error("get pic width and height error", e); } finally { IOUtils.closeQuietly(fileOutputStream); }

出现的异常:

javax.imageio.IIOException: Error reading PNG image data at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1345) at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1614) at javax.imageio.ImageIO.read(ImageIO.java:1448) at javax.imageio.ImageIO.read(ImageIO.java:1308) at me.ele.commodity.spu.service.impl.CommodityDataProcessServiceImpl.downloadPicture(CommodityDataProcessServiceImpl.java:164) at me.ele.commodity.spu.service.impl.CommodityDataProcessServiceImpl.getCheckedPicList(CommodityDataProcessServiceImpl.java:207) at me.ele.commodity.spu.service.impl.CommodityDataProcessServiceImpl.asyncPictureListAndDataLog(CommodityDataProcessServiceImpl.java:88) at sun.reflect.GeneratedMethodAccessor327.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:748) Caused by: java.io.EOFException: Unexpected end of ZLIB input stream at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240) at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at java.io.DataInputStream.readFully(DataInputStream.java:195) at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1119) at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1223) at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1338) ... 17 more

当往输出流fileOutputStream里写数据的时候,如果fileOutputStream没有及时的close掉,导致数据不完整。在一个流里,close操作可以将缓冲的数据给冲出来,或会在生成的文件里填充一些信息。所以往输出流里写数据然后到一个具体的文件,及时的close掉输出流是很有必要的。

另外这个代码也是有问题的,并发访问的时候,如果图片文件名相同也会导致生成的图片出现问题,线程1读取data byte array生成了图片1,准备去读取图片1,然后线程2开始生成相同文件名的图片去覆盖图片1,然后线程1读取到了数据不完整的图片1 而出现异常。

可以参考另外一个例子: http://blog.csdn.net/Revivedsun/article/details/52073554

转载请注明原文地址: https://www.6miu.com/read-30009.html

最新回复(0)