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