3

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:

Request Headers - Accept-Encoding

Response is NOT - missing Content-Encoding header: Response Headers

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
miro
  • 149
  • 3
  • 11

1 Answers1

1

I just found out that it is actually working :). I was using BrowserSync before to test it out and it's not working with BrowserSync for some reason.

miro
  • 149
  • 3
  • 11