REST响应后如何删除文件 [英] How to delete file after REST response

查看:101
本文介绍了REST响应后如何删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将文件作为对REST请求的响应返回后处理删除文件的最佳方法是什么?

What is the best way to handle deleting a file after it has been returned as the response to a REST request?

我有一个创建文件的端点请求并在响应中返回它。一旦调度响应,就不再需要该文件,可以/应该删除。

I have an endpoint that creates a file on request and returns it in the response. Once the response has been dispatched the file is no longer needed and can/should be removed.

@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文件,但我会以为有更优雅的机制来实现这一目标。该文件可能非常大,所以我无法将其加载到内存中。

I guess I could create a tmp file but I would have thought there was a more elegant mechanism to achieve this. The file could be quite large so I cannot load it into memory.

推荐答案

有一个更优雅的解决方案,不要写文件,只是直接写入 Response 实例中包含的输出流。

There is a more elegant solution, don't write a file, just write directly to the output stream contained in the instance of Response.

这篇关于REST响应后如何删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆