GZipOutputStream 和 GZipOutputStream应用引擎
我正在 AppEngine 上编写一个 java servlet。该 servlet 生成 png 图像。我想“gzip”响应。我这样做:
resp.setHeader("Content-Encoding","gzip");
resp.setContentType("image/png");
// ... png generation ...
GZIPOutputStream gzos = new GZIPOutputStream(resp.getOutputStream());
gzos.write(myPNGdata);
gzos.close();
但是:在开发服务器中,没关系,png 显示正常,并且响应被很好地压缩。然后我在生产服务器(AppEngine)上进行测试,我得到的只是一个“损坏的”图像...
什么可以我的代码有问题吗?与开发/生产环境有关吗?
当然,如果我不压缩输出,那么在两种环境中都可以。
感谢您的任何帮助。
编辑:我也尝试过这个:
GZIPOutputStream gzos = new GZIPOutputStream(resp.getOutputStream());
gzos.write(ImagesServiceFactory.makeImage(readImage("somePicture.png")).getImageData());
gzos.flush();
gzos.finish();
gzos.close();
但它也不起作用。
编辑2:事实上,响应是gzip。我用“curl theUrl > tmp.gz”获取了 servlet,然后我枪压缩“tmp.gz”,图像很好。但没有浏览器可以正确显示它:( 我的 gzip 有什么问题吗?
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...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
App Engine 基础架构将在适当的时候为您处理 gzip 内容。你不应该自己做。
The App Engine infrastructure will take care of gzipping content for you when appropriate. 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.