java-使用jax-rs发送临时文件
作者:互联网
我正在尝试使用jax-rs发送一个临时文件,并在下载完成后删除该临时文件.为此,我将InputSream细分为子类,以便在流关闭时得到通知.这是我到目前为止的内容:
@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();
}
}
不幸的是,一旦下载完成,就不会调用close().我想念什么吗?
解决方法:
更改
Response.ResponseBuilder response = Response.ok((Object) file);
至
Response.ResponseBuilder response = Response.ok(inputStream);
标签:rest,jax-rs,file,java 来源: https://codeday.me/bug/20191120/2044570.html