I am trying to send a temporary file with jax-rs and delete the temporary file once the download is done. For that purpose I subclassed InputSream in order to be notified once the stream is closed. This is what I have so far:
@GET
@Path("download/{fileName}")
public Response downloadFile(@PathParam("fileName") String fileName) {
InputStream inputStream = new InputStreamWithFileDeletion(new getFile(filename));
Response.ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename="+"fileName"+".xls");
return response.build();
}
InputStreamWithFileDeletion:
public class InputStreamWithFileDeletion extends FileInputStream {
File f;
public InputStreamWithFileDeletion(File file) throws FileNotFoundException {
super(file);
f = file;
}
@Override
public void close() throws IOException {
super.close();
f.delete();
}
}
Unfortunately, once the download is done, close() is not called. Am I missing something?