24

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!

}

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.

tarka
  • 5,289
  • 10
  • 51
  • 75

5 Answers5

25

Use a StreamingOutput as entity:

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);
        }
    }
}
cmaulini
  • 401
  • 1
  • 5
  • 9
9

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

  • 7
    could you provide details about writing directly to output stream? – Duc Tran Sep 14 '16 at 22:13
  • 1
    I agree it misses more details or example to be the accepted answer. Also I would not set this as a duplicated question (both are similar but this one could be useful also for an already existing file). – Aleix Feb 05 '21 at 08:10
2

I did something like this recently in rest service development using jersey

@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();
    }
Praneeth
  • 559
  • 9
  • 19
-1

save the response in a tmp variable with replacing your return statement like this:

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

send file name on response:

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

after that you can send delete restful method

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

    File delete = new File(file);

    delete.delete();

}
  • 1
    This would mean the client then has to know to tell the server to delete its file (which is really the servers problem, not the clients). And also provides a handy method for anyone with access to the API to delete files on the server - also not a great idea – Matt Harrison Jun 07 '19 at 02:23