Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a file after sending it in a web app?

I have a web application. I am using java and spring. The application can create a file and send it to the browser, this is working fine. The way I do it is:

I create the file in a Services class, and the method returns the address to the controller. The controller then sends the file, and it is downloaded correctly. The code for the controller method is this.

@RequestMapping("/getFile")
public @ResponseBody
FileSystemResource getFile() {

    String address = Services.createFile();
    response.setContentType("application/vnd.ms-excel");
    return new FileSystemResource(new File (address));
}

The problem is that the file is saved in the server, and after many requests it will have a lot of files. I have to delete them manually. The question is: How can I delete this file after sending it? or Is there a way to send the file without saving it in the server?

like image 911
ia.solano Avatar asked Jul 29 '13 20:07

ia.solano


1 Answers

Don't use @ResponseBody. Have Spring inject the HttpServletResponse and write directly to its OutputStream.

@RequestMapping("/getFile")
public void getFile(HttpServletResponse response) {
    String address = Services.createFile();
    File file = new File(address);
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment; filename=" + file.getName());

    OutputStream out = response.getOutputStream();
    FileInputStream in = new FileInputStream(file);

    // copy from in to out
    IOUtils.copy(in,out);

    out.close();
    in.close();
    file.delete();
}

I haven't added any exception handling. I leave that to you.

FileSystemResource is really just is a wrapper for FileInputStream that's used by Spring.

Or, if you want to be hardcore, you could make your own FileSystemResource implementation with its own getOutputStream() method that returns your own implementation of FileOutputStream that deletes the underlying file when you call close() on it.

like image 138
Sotirios Delimanolis Avatar answered Sep 20 '22 10:09

Sotirios Delimanolis