3

我正在尝试使用 Spring Boot REST 提供压缩后的日志文件。
文件已经被压缩了,我不需要压缩它们。
浏览器应该解压缩它们并显示纯文本。

根据我在谷歌上搜索的信息,我需要两件事才能使其工作:

  • 在请求标头中设置以下内容:'Accept-Encoding': 'gzip'
  • 在响应标头中设置以下内容:'Content-Encoding': 'gzip'

请求正常:

请求标头 - 接受编码

响应不是 - 缺少 Content-Encoding 标头: 响应标头

这是我的Java代码:

@RequestMapping(value = "/download",
                method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> download(HttpServletRequest request, HttpServletResponse response) {
    log.debug("REST request to get the filesystem resource");

    // hardcoded for testing purpose
    File f = new File("C:/file.log.gz");
    InputStream inputStream = null;
    InputStreamResource inputStreamResource = null;
    HttpHeaders headers = null;
    try {
        inputStream = new FileInputStream(f);
        inputStreamResource = new InputStreamResource(inputStream);

        headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Content-Encoding", "gzip");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return ResponseEntity
        .ok()
        .headers(headers)
        .contentType(MediaType.parseMediaType("text/plain"))
        .body(inputStreamResource);
}

Spring 是否从响应中删除了这个特定的标头?

4

1 回答 1

1

我刚刚发现它实际上正在工作:)。我之前使用 BrowserSync 来测试它,但由于某种原因它不能与 BrowserSync 一起使用。

于 2016-01-21T08:02:58.987 回答