• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem while reading PNG image through ImageIO API

Amrut Chaudhary
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am using the commons-fileupload API to upload images.

I first write the image to disk and then load it via ImageIO.read(new FileInputStream("my.png")) it works fine for some PNG images but for some PNG image it gives me error like.

javax.imageio.IIOException: Error reading PNG image data
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1291)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1560)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at com.asite.imageio.ImageIOTest.readImage(ImageIOTest.java:45)
at com.asite.imageio.ImageIOTest.main(ImageIOTest.java:27)
Caused by: java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:223)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at java.io.DataInputStream.readFully(DataInputStream.java:178)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1075)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1179)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1284)
... 5 more

Here is the code which i wrote to read image

BufferedImage image =ImageIO.read(new FileInputStream("abc.png"));
int width = image.getWidth();
int height = image.getHeight();
System.out.println("width : "+width+"\nheight : "+height);

Lester Burnham
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, have you made sure that after the upload, the files are bit-for-bit identical copies of the original files?

Secondly, be aware that web apps do not work well (or at all) with relative file paths (like "abc.png"); you should switch to using absolute paths (maybe by including the web app context root).
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic