2014-11-14 17 views
11

Cách tốt nhất để xử lý việc xóa tệp sau khi nó được trả về như là phản hồi cho yêu cầu REST là gì?Cách xóa tệp sau khi phản hồi REST

Tôi có điểm cuối tạo tệp theo yêu cầu và trả về trong phản hồi. Khi phản hồi đã được gửi đi, tệp không còn cần thiết và có thể/nên được loại bỏ.

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

} 

Tôi đoán tôi có thể tạo tệp tmp nhưng tôi đã nghĩ rằng có một cơ chế thanh lịch hơn để đạt được điều này. Tệp có thể khá lớn nên tôi không thể tải nó vào bộ nhớ.

+1

Không chắc chắn tại sao bỏ phiếu xuống! – tarka

Trả lời

8

giải pháp thanh lịch hơn, không viết tệp, chỉ cần viết trực tiếp vào luồng đầu ra có trong ví dụ Response.

+0

bạn có thể cung cấp chi tiết về cách viết trực tiếp vào luồng đầu ra không? – Silentbang

-1

lưu phản ứng trong một biến tmp với thay thế câu lệnh return bạn như thế này :

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

tên file gửi về phản ứng:

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

sau đó bạn có thể gửi xóa phương pháp yên tĩnh

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

    File delete = new File(file); 

    delete.delete(); 

} 
0

tôi đã làm một cái gì đó như thế này thời gian gần đây trong việc phát triển dịch vụ còn lại sử dụng 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(); 
    } 
7

Sử dụng một StreamingOutput như một thực thể:

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); 
     } 
    } 
} 
Các vấn đề liên quan