【发布时间】:2018-07-12 03:26:39
【问题描述】:
我想在创建 zip 文件后清理文件。其中一项操作是删除 zip 文件本身。
@RequestMapping(path = "/downloadZip", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String fileName) throws IOException {
//Create a zip file and add entries
Path path = Paths.get(file.getAbsolutePath());
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
//Call a cleanup method
//cleanUp();
}
在 Junit 测试中,我能够使用静态方法 AfterClass 删除 zip 文件。
问题:如何定义返回后获取的方法。
谢谢!
【问题讨论】:
-
对资源使用 try:stackoverflow.com/questions/17650970/…
-
将方法的内容包装在
try / finally块中,并在finally块中进行清理。此外,您不应该将整个文件读入内存,而是直接将其从文件系统流式传输到客户端,这将为您节省大量内存。 -
我能够在 finally 块中定义清理,我将在未来的迭代中按照@Snickers3192 的建议研究 AspectJ。缓冲区不起作用的原因是,我们正在编辑很多文件 ~ 200 MB,然后进行更改,例如将文件转换为 PDF,更改文件名。谢谢!
标签: java spring rest spring-boot return