ocramot
ocramot

Reputation: 1391

Java IIOException caused by ZipException while trying to convert from String to PNG

My Java Servlet has a String coming from a client (all I know is that he is actually sending me a PNG as a parameter in a request with contentType = application/x-www-form-urlencoded), and I'm trying to convert it back to a File and save it. Here is my code (based on this):

byte[] data = org.apache.commons.codec.binary.Base64.decodeBase64(request.getParameter("image").getBytes());
String filename = request.getParameter("filename");

try{
    java.awt.image.BufferedImage image = javax.imageio.ImageIO.read(new ByteArrayInputStream(data));
    javax.imageio.ImageIO.write(image, "png", new File(getUploadFolder() + filename + ".png"));
}catch(Exception e){
    System.out.println("Exception saving image: " + e.getMessage());
    e.printStackTrace();
}

And here is my error stacktrace:

Exception saving image: Error reading PNG image data
javax.imageio.IIOException: Error reading PNG image data
    at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1308)
    at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1577)
    at javax.imageio.ImageIO.read(ImageIO.java:1448)
    at javax.imageio.ImageIO.read(ImageIO.java:1352)
    at com.mypackage.servlet.UploadServlet.doPost(UploadServlet.java:113)
Caused by: java.util.zip.ZipException: invalid code lengths set
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at java.io.FilterInputStream.read(FilterInputStream.java:83)
    at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1085)
    at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1196)
    at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1301)
    ... 4 more

Looking at the InflaterInputStream class code, I see that it is actually a DataFormatException:

public int read(byte[] b, int off, int len) throws IOException {
    ensureOpen();
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }
    try {
        int n;
        while ((n = inf.inflate(b, off, len)) == 0) {
            if (inf.finished() || inf.needsDictionary()) {
                reachEOF = true;
                return -1;
            }
            if (inf.needsInput()) {
                fill();
            }
        }
        return n;
    } catch (DataFormatException e) {
        String s = e.getMessage();
        throw new ZipException(s != null ? s : "Invalid ZLIB data format");
    }
}

But this is not really so enlightening.

Upvotes: 0

Views: 768

Answers (1)

Harald K
Harald K

Reputation: 27064

As @leonbloy says, there's no need to decode and then re-encode the data.

If you are sure the PNG file is good*, you can simply write the data to file, as follows, and you should be done.

byte[] data = org.apache.commons.codec.binary.Base64.decodeBase64(request.getParameter("image").getBytes());
String filename = request.getParameter("filename");

OutputStream output = new FileOutputStream(new File(getUploadFolder() + filename + ".png"));

try {
    output.write(data);
}
finally {
    output.close();
}

*) The evidence provided so far suggests otherwise, and the code you were using should work (even if it's a waste of CPU cycles). It would be very interesting to see a sample of the data producing the exception you see, to figure out the cause of the exception.

Upvotes: 1

Related Questions