使用Spring引导服务gzip文件
问题描述:
我想使用Spring引导REST服务gzip日志文件。 文件已经gzip,我不需要gzip他们。 浏览器应解压缩并显示纯文本。 根据我GOOGLE了信息,我需要两样东西,使其工作:使用Spring引导服务gzip文件
- 坐落在请求头以下:
'Accept-Encoding': 'gzip'
- 设置响应报头中的以下内容:
'Content-Encoding': 'gzip'
请求OK:
这里是我的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);
}
是春天从响应删除这个特定的头?
答
我刚发现它实际上在工作:)。之前我使用BrowserSync进行测试,出于某种原因它不适用于BrowserSync。