2016-12-28 44 views
1

以下のコードは、実際にファイル名と拡張子(ここでは.zip拡張子)なしでダウンロードする代わりに、常にf.txtファイルをダウンロードしています。.zipファイルをf.txtファイルとしてダウンロードしました - springboot

@RequestMapping(value = "/files/{fileId}", method = RequestMethod.GET, produces = "application/zip") 
    public ResponseEntity<Resource> downloadFile(@PathVariable("fileId") String fileName) { 
     log.info("Downloading file..!!!!"); 
     HttpHeaders headers = new HttpHeaders(); 
     headers.setContentType(MediaType.valueOf("application/zip")); 
     log.info("Content info : "+headers.getContentType().toString()); 
     File file = FileUtils.getFile("backup/" + fileName + ".zip"); 
     log.info("File name is : "+file.getName()); 
     FileSystemResource fileSystemResource = new FileSystemResource(file); 

     return new ResponseEntity<>(fileSystemResource, headers, HttpStatus.OK); 
    } 

誰かが私に間違いがあるか/いくつかの修正が行われることを教えてもらえますか?

答えて

5

f.txtは、Content-Disposition応答ヘッダーから来ています。これは、問題を解決するにはcve-2015-5211(RFDアタック)

+4

だから、 'headers.setContentDispositionFormData( "添付ファイル"、 "my.zip")を使用します;」など。 –

1

を固定の結果であり、content-dispositioncontent-lengthヘッダを追加:

... 
log.info("File name is : "+file.getName()); 

// Adding the following two lines should fix the download for you: 
headers.set("content-disposition", "inline; filename=\"" + file.getName() + "\""); 
headers.set("content-length", String.valueOf(file.length())); 

FileSystemResource fileSystemResource = new FileSystemResource(file); 
... 
関連する問題