简体   繁体   English

GZipOutputStream和Appengine

[英]GZipOutputStream & appengine

I'm writing a java servlet on AppEngine. 我正在AppEngine上编写Java Servlet。 This servlet generates png images. 该servlet生成png图像。 I would like to "gzip" the response. 我想“ gzip”的答复。 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. 但是:在开发服务器中,没关系,png可以正常显示,并且响应也可以很好地压缩。 Then I test on production server (AppEngine) and all I get is a "broken" image... 然后我在生产服务器(AppEngine)上进行测试,得到的只是一个“损坏”的图像...

替代文字

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. 当然,如果我不gzip输出,在两种环境中都可以。

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. 编辑2:实际上,响应是gzip。 I fetched the servlet with "curl theUrl > tmp.gz", then I gunzip "tmp.gz", and the image is fine. 我使用“ curl theUrl> tmp.gz”获取了该servlet,然后将“ tmp.gz”打包为zip,并且图像很好。 But no browser can display it correctly :( What's wrong with my gzip? 但是没有浏览器可以正确显示它:(我的gzip有什么问题?

The App Engine infrastructure will take care of gzipping content for you when appropriate. App Engine基础架构将在适当时为您提供gzipping内容。 You shouldn't do it yourself. 你不应该自己做。

Check the size of your downloaded image. 检查下载图像的大小。 If it is smaller then you expecting, most likely you need to flush the stream before closing. 如果小于预期,则很可能需要在关闭流之前冲洗流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM