【发布时间】:2015-01-11 20:27:36
【问题描述】:
在文件作为对 REST 请求的响应返回后处理删除文件的最佳方法是什么?
我有一个端点,可以根据请求创建文件并在响应中返回它。发送响应后,不再需要该文件,可以/应该删除该文件。
@Path("file")
@GET
@Produces({MediaType.APPLICATION_OCTET_STREAM})
@Override
public Response getFile() {
// Create the file
...
// Get the file as a steam for the entity
File file = new File("the_new_file");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename=\"the_new_file\"");
return response.build();
// Obviously I can't do this but at this point I need to delete the file!
}
我想我可以创建一个 tmp 文件,但我认为有一个更优雅的机制来实现这一点。该文件可能非常大,因此我无法将其加载到内存中。
【问题讨论】:
-
不知道为什么投反对票!