is there an existing FileInputStream delete on close?

前端 未结 4 986
夕颜
夕颜 2020-12-15 16:52

Is there an existing way to have a FileInputStream delete the underlying file automatically when closed?

I was planning to make my own utility class t

4条回答
  •  忘掉有多难
    2020-12-15 17:51

    I know this is an old question, but I just ran into this issue, and found another answer: javax.ws.rs.core.StreamingOutput.

    Here's how I used it:

        File downloadFile = ...figure out what file to download...
        StreamingOutput so = new StreamingOutput(){
             public void write(OutputStream os) throws IOException {
                FileUtils.copyFile(downloadFile, os);
                downloadFile.delete();
        }
    
        ResponseBuilder response = Response.ok(so, mimeType);
        response.header("Content-Disposition", "attachment; filename=\""+downloadFile.getName()+"\"");
        result = response.build();
    

提交回复
热议问题