I'm trying to serve gzipped log files using Spring Boot REST.
Files are already gzipped, I don't need to gzip them.
Browser should unzip them and show plain text.
Based on the info I googled, I need two things to make it work:
- Set the following in request header:
'Accept-Encoding': 'gzip'
- Set the following in response header:
'Content-Encoding': 'gzip'
Request is OK:
Response is NOT - missing Content-Encoding header:
Here is my Java code:
@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);
}
Is Spring removing this specific header from response?