我正在尝试使用 Spring Boot REST 提供 gzip 压缩的日志文件。
文件已经被 gzip 压缩,我不需要 gzip 它们。
浏览器应该解压缩它们并显示纯文本。
根据我在谷歌上搜索到的信息,我需要两件事才能使其发挥作用:
- 在请求 header 中设置以下内容:
'Accept-Encoding': 'gzip'
- 在响应 header 中设置以下内容:
'Content-Encoding': 'gzip'
请求正常:
这是我的 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 是否从响应中删除此特定 header ?
最佳答案
我刚刚发现它确实有效:)。我之前使用过 BrowserSync 来测试它,但由于某种原因它不能与 BrowserSync 一起使用。
关于java - 使用 Spring Boot 提供 gzip 压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34905839/