3

ファイルのダウンロード用に ResponseEntity を返しています。

@RequestMapping("/download")
public ResponseEntity<byte[]> download(){
    byte[] fileContent = manager.getFile();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.setPragma("cache");
    headers.setExpires(0);
    headers.setCacheControl("private");
    headers.setContentDispositionFormData("attachment", "sample.pdf");
    headers.setContentLength(fileContent.length);
    return new ResponseEntity<byte[]>(fileContent, headers, HttpStatus.OK); 
}

クロムでは、リクエストヘッダーをフォローしています:

Cache-Control:no-store
Cache-Control:no-cache
Cache-Control:private
Content-Disposition:form-data; name="attachment"; filename="sample-pdf.pdf"
Content-Length:1469
Content-Type:application/pdf;charset=UTF-8
Date:Thu, 15 Aug 2013 08:10:25 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:cache
Pragma:no-cache
Server:Apache-Coyote/1.1

HttpServletResponse を使用して古い方法で行う場合

public void download(HttpServletResponse response){
    byte[] fileContent = manager.getFile();
    response.reset();
    response.setContentType("application/pdf");
    response.setHeader("Pragma", "cache");
    response.setHeader("Expires", "0");
    response.setHeader("Cache-control", "private");
    response.setHeader("Content-Disposition", "attachment; filename=sample.pdf");
    FileCopyUtils.copy(fileContent, response.getOutputStream());
}

ヘッダーは私が欲しかったものです

Cache-Control:private
Content-Disposition:form-data; name="attachment"; filename="sample-pdf.pdf"
Content-Length:1469
Content-Type:application/pdf;charset=UTF-8
Date:Thu, 15 Aug 2013 08:10:25 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:cache
Server:Apache-Coyote/1.1

使用時に http ヘッダー値をクリーンアップする方法はありますResponseEntityか?

4

1 に答える 1

0

使用禁止

headers.setContentDispositionFormData("attachment", "sample.pdf");

または他のセット

次のような add メソッドのみを使用します。

headers.add("Content-Disposition", "inline;filename=\"sample_"+System.currentTimeMillis()+".pdf\"");
于 2013-09-24T18:47:15.597 に答える