用jax-rs发送临时文件 [英] Send temp file with jax-rs

查看:121
本文介绍了用jax-rs发送临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jax-rs发送一个临时文件,并在下载完成后删除该临时文件.为此,我将InputSream细分为子类,以便在流关闭时得到通知.这是我到目前为止所拥有的:

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:

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().我想念什么吗?

Unfortunately, once the download is done, close() is not called. Am I missing something?

推荐答案

更改

Response.ResponseBuilder response = Response.ok((Object) file);

Response.ResponseBuilder response = Response.ok(inputStream);

这篇关于用jax-rs发送临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆