2014-11-14 13 views
11

Qual è il modo migliore per gestire l'eliminazione di un file dopo che è stato restituito come risposta a una richiesta REST?Come eliminare il file dopo la risposta REST

Ho un endpoint che crea un file su richiesta e lo restituisce nella risposta. Una volta che la risposta è stata inviata, il file non è più necessario e può/deve essere rimosso.

@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! 

} 

Immagino di poter creare un file tmp ma avrei pensato che esistesse un meccanismo più elegante per raggiungere questo obiettivo. Il file potrebbe essere abbastanza grande, quindi non posso caricarlo in memoria.

+1

Non sicuro perché il voto negativo! – tarka

risposta

8

C'è una soluzione più elegante, non scrivere un file, basta scrivere direttamente sul flusso di output contenuto nell'istanza di Response.

+0

potresti fornire dettagli sulla scrittura direttamente nello stream di output? – Silentbang

-1

salvare la risposta in una variabile tmp con la sostituzione la sua dichiarazione di ritorno come questo :

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

nome del file di invio della risposta:

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

dopo che è possibile inviare eliminare metodo riposante

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

    File delete = new File(file); 

    delete.delete(); 

} 
0

ho fatto qualcosa di simile di recente nello sviluppo dei servizi di riposo utilizzando maglia

@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(); 
    } 
7

Utilizzare uno StreamingOutput come un'entità:

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); 
     } 
    } 
} 
Problemi correlati