1

I'm writing a java servlet on AppEngine. This servlet generates png images. I would like to "gzip" the response. I do it this way:

resp.setHeader("Content-Encoding","gzip");
resp.setContentType("image/png");

// ... png generation ...

GZIPOutputStream gzos = new GZIPOutputStream(resp.getOutputStream());
gzos.write(myPNGdata); 
gzos.close();

But: in development server, it's ok, the png displays fine and the response is well gzipped. Then I test on production server (AppEngine) and all I get is a "broken" image...

alt text

What could be wrong with my code? Is it related to dev/prod environment?

Off course, If I don't gzip the output, it's ok in both environments.

Thanks for any help.


Edit: I tried this too:

GZIPOutputStream gzos = new GZIPOutputStream(resp.getOutputStream());
gzos.write(ImagesServiceFactory.makeImage(readImage("somePicture.png")).getImageData());
gzos.flush();
gzos.finish();
gzos.close();

and it doesn't work either.


Edit 2: in fact, the response is gzip. I fetched the servlet with "curl theUrl > tmp.gz", then I gunzip "tmp.gz", and the image is fine. But no browser can display it correctly :( What's wrong with my gzip?

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Francois
  • 10,730
  • 7
  • 47
  • 80

2 Answers2

3

The App Engine infrastructure will take care of gzipping content for you when appropriate. You shouldn't do it yourself.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • I know that but I can't change the requests headers from the client side (legacy app). But as it's working in development server, it should work on real app engine, shouldn't it? – Francois Dec 15 '10 at 07:33
  • @Francois It's a bug if it works in the dev_appserver - because you cannot set these headers yourself in production. – Nick Johnson Dec 16 '10 at 23:34
0

Check the size of your downloaded image. If it is smaller then you expecting, most likely you need to flush the stream before closing.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • I tried with `gzos.flush();` and `gzos.finish();` but it is the same. Ok on dev server, KO on app engine. – Francois Dec 14 '10 at 21:56