3

Spring Boot REST を使用して gzip 圧縮されたログ ファイルを提供しようとしています。
ファイルは既に gzip されています。gzip する必要はありません。
ブラウザはそれらを解凍し、プレーンテキストを表示する必要があります。

私がグーグルで調べた情報に基づいて、それを機能させるには2つのことが必要です:

  • リクエスト ヘッダーに次のように設定します。'Accept-Encoding': 'gzip'
  • 応答ヘッダーに次を設定します。'Content-Encoding': 'gzip'

リクエストはOKです:

リクエスト ヘッダー - Accept-Encoding

応答はありません - 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