【发布时间】:2015-04-09 21:59:45
【问题描述】:
我的应用程序中有一个压缩过的 JSON 资源,我想通过 servlet 提供它。浏览器不同意确切的原因,但它们都不会真正加载我的内容。这段代码看起来有问题吗?
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(200);
InputStream in = <get an input stream to my gzipped data>;
resp.setIntHeader("Content-Length", in.available());
OutputStream out = resp.getOutputStream();
out.write(IOUtils.toByteArray(in));
out.close();
resp.setHeader("Content-Encoding", "gzip");
resp.setHeader("Content-Type", "application/json");
}
}
【问题讨论】: