在春季发送给用户后如何删除文件

时间:2018-12-14 09:18:04

标签: java spring spring-mvc java-8

将文件发送给用户作为响应后,如何自动删除文件?

我正在使用Spring MVC 5

@GetMapping("/download")
public ResponseEntity<InputStreamResource> download(@RequestParam String fileName) throws FileNotFoundException {
        File tempLocation = new File("tmp/" + fileName);
        myservice.downloadObject(fileName, tempLocation);

        InputStreamResource resource = new InputStreamResource(new FileInputStream(downloaded));

        return ResponseEntity.ok()
                             .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + downloaded.getName())
                             .contentType(MediaType.APPLICATION_OCTET_STREAM)
                             .contentLength(downloaded.length())
                             .body(resource);
    }

0 个答案:

没有答案