GZipOutputStream 和 GZipOutputStream应用引擎

发布于 2024-10-07 11:03:36 字数 948 浏览 0 评论 0原文

我正在 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)上进行测试,我得到的只是一个“损坏的”图像...

alt text

什么可以我的代码有问题吗?与开发/生产环境有关吗?

当然,如果我不压缩输出,那么在两种环境中都可以。

感谢您的任何帮助。


编辑:我也尝试过这个:

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...

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

a√萤火虫的光℡ 2024-10-14 11:03:36

App Engine 基础架构将在适当的时候为您处理 gzip 内容。你不应该自己做。

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

完美的未来在梦里 2024-10-14 11:03:36

检查下载的图像的大小。如果它比您预期的小,很可能您需要在关闭之前刷新流。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文