Quantcast
Channel: User blacktide - Stack Overflow
Viewing all articles
Browse latest Browse all 49

Answer by blacktide for How to delete a file after sending it in a web app?

$
0
0

Here's a simple way using StreamingResponseBody from your controller method:

File file = getFile();return ResponseEntity.ok()    .headers(headers)    .contentLength(length)    .contentType(MediaType.APPLICATION_OCTET_STREAM)    .body(os -> {        Files.copy(file.toPath(), os);        Files.delete(file.toPath());    });

The lambda is shorthand for the following:

new StreamingResponseBody() {    @Override    public void writeTo(OutputStream os) throws IOException {        Files.copy(file.toPath(), os);        Files.delete(file.toPath());    }}

Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>