Java 通过HTTP获取静态GZIP文件和自动解压缩只在IE中起作用-为什么?

Java 通过HTTP获取静态GZIP文件和自动解压缩只在IE中起作用-为什么?,java,internet-explorer,http,google-chrome,Java,Internet Explorer,Http,Google Chrome,因此,我向服务器请求一个静态*.gz文件,如下所示: Request URL:https://1.2.3.4:9090/arch/archive.csv.gz Request Method:GET //.... HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; String path

因此,我向服务器请求一个静态
*.gz
文件,如下所示:

Request URL:https://1.2.3.4:9090/arch/archive.csv.gz
Request Method:GET
    //....

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    String path = req.getServletPath();

        res.setHeader("Content-Encoding", "gzip");
        res.setHeader("Content-Type", "text/plain");
        res.setContentType("text/plain; charset=UTF-8");
        String filename = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
        res.setHeader("Content-Disposition", "attachment; filename=" + filename);
   //....
我希望当用户试图从浏览器访问文件时,该文件自动缩小并保存为csv,因此我在过滤器中设置响应标题,如下所示:

Request URL:https://1.2.3.4:9090/arch/archive.csv.gz
Request Method:GET
    //....

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    String path = req.getServletPath();

        res.setHeader("Content-Encoding", "gzip");
        res.setHeader("Content-Type", "text/plain");
        res.setContentType("text/plain; charset=UTF-8");
        String filename = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
        res.setHeader("Content-Disposition", "attachment; filename=" + filename);
   //....
因此,在发出get请求后,我在Chrome开发者工具中看到以下响应:

* Response Headers *:
Accept-Ranges:bytes
Content-Disposition:filename=archive.csv
Content-Encoding:gzip
Content-Length:342208
Content-Type:application/x-gzip;charset=UTF-8
Date:Wed, 08 Jan 2014 09:54:35 GMT
ETag:W/"342208-1389113112000"
Last-Modified:Tue, 07 Jan 2014 16:45:12 GMT
Server:Apache-Coyote/1.1
这在Internet Explorer中非常有效-文件被下载并保存为CSV-一切正常。然而,当我尝试在Chrome/Firefox中执行同样的操作时,该文件被保存为CSV文件,但没有解压缩(尽管有CSV扩展名,但该文件实际上是一个GZ归档文件)


为什么会这样?如何使其在所有浏览器下都能工作?

我刚刚使用您在上面指定的相同标题运行了一个测试,并且csv文件在Chrome/Firefox中都可以很好地解压缩。不过,我运行的是Ubuntu,而不是Windows。也许这是一个平台(特定于Windows)问题?我的平台是Windows 7 Prof、SP1、x64。Chrome版本为31.0.1650.63 mtry:从编码中删除gzip,将“.gz”添加到文件名,将内容类型更改为“application/x-gzip”