Serve Gzipped content with Java Servlets

2019-02-13 02:35发布

I was wondering if there was an easy way to serve GZipped content with Java Servlets. I already have the app up and running so the modifications needed should be too heavy.

I have access to the response object just at the end of the doPost/doGet method, so I'm looking for something like

response.setGzip(true);

It doesn't have to be that easy but it would be ideal.

Thanks a lot

7条回答
Evening l夕情丶
2楼-- · 2019-02-13 03:33

Look at GzipOutputStream class. Something like this:

response.setContentType(...)
GzipOutputStream os = new GzipOutputStream(response.getOutputStream);
//write to os
Writer writer = new PrintWriter(os);

Then use writer like you do usually.

查看更多
登录 后发表回答