【问题标题】:How to delete file after REST response [duplicate]REST响应后如何删除文件[重复]
【发布时间】: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 文件,但我认为有一个更优雅的机制来实现这一点。该文件可能非常大,因此我无法将其加载到内存中。

【问题讨论】:

  • 不知道为什么投反对票!

标签: java rest resteasy


【解决方案1】:

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

【讨论】:

  • 您能否提供有关直接写入输出流的详细信息?
  • 我同意它遗漏了更多细节或示例作为接受的答案。此外,我不会将其设置为重复的问题(两者相似,但这个问题对于已经存在的文件也很有用)。
【解决方案2】:

将响应保存在 tmp 变量中,并像这样替换您的 return 语句:

Response res = response.build();
//DELETE your files here.
//maybe this is not the best way, at least it is a way.
return res;

【讨论】:

    【解决方案3】:

    响应时发送文件名:

    return response.header("filetodelete", FILE_OUT_PUT).build();
    

    之后你可以发送删除restful方法

    @POST
    @Path("delete/{file}")
    @Produces(MediaType.TEXT_PLAIN)
    public void delete(@PathParam("file") String file) {
    
        File delete = new File(file);
    
        delete.delete();
    
    }
    

    【讨论】:

    • 这意味着客户端必须知道告诉服务器删除它的文件(这实际上是服务器的问题,而不是客户端的问题)。并且还为任何有权访问 API 的人提供了一种方便的方法来删除服务器上的文件——这也不是一个好主意
    【解决方案4】:

    我最近在使用球衣的休息服务开发中做了这样的事情

    @GET
    @Produces("application/zip")
    @Path("/export")
    public Response exportRuleSet(@QueryParam("ids") final List<String> ids) {
    
        try {
            final File exportFile = serviceClass.method(ruleSetIds);
    
            final InputStream responseStream = new FileInputStream(exportFile);
    
    
            StreamingOutput output = new StreamingOutput() {
                @Override
                public void write(OutputStream out) throws IOException, WebApplicationException {  
                    int length;
                    byte[] buffer = new byte[1024];
                    while((length = responseStream.read(buffer)) != -1) {
                        out.write(buffer, 0, length);
                    }
                    out.flush();
                    responseStream.close();
                   boolean isDeleted = exportFile.delete();
                    log.info(exportFile.getCanonicalPath()+":File is deleted:"+ isDeleted);                 
                }   
            };
            return Response.ok(output).header("Content-Disposition", "attachment; filename=rulset-" + exportFile.getName()).build();
        }
    

    【讨论】:

      【解决方案5】:

      使用 StreamingOutput 作为实体:

      final Path path;
      ...
      return Response.ok().entity(new StreamingOutput() {
          @Override
          public void write(final OutputStream output) throws IOException, WebApplicationException {
              try {
                  Files.copy(path, output);
              } finally {
                  Files.delete(path);
              }
          }
      }
      

      【讨论】:

      • 这工作完美,应该被接受,因为提供了一个工作示例!
      猜你喜欢
      • 2021-08-06
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 2012-10-23
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多