.zip文件下载为f.txt文件 - springboot

har*_*vmb 4 java download spring-boot

下面的代码总是下载一个f.txt文件,而不是下载没有实际的文件名和扩展名(这里是.zip扩展名).

@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);
    }
Run Code Online (Sandbox Code Playgroud)

如果有人能让我知道错误在哪里/要做一些修改,那会很棒吗?

Ste*_*oll 7

f.txt来自Content-Disposition响应头.这是修复cve-2015-5211(RFD攻击)的结果

  • 所以使用`headers.setContentDispositionFormData("attachment","my.zip");`等等. (4认同)