I need to generate a file for download for a user, imagine, you should give a serial number as a text file to your user. After searching I achieve this solution that works perfectly:
@GetMapping(path = "/file/get", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity verificationFile() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
headers.setContentDispositionFormData("attachment", "test.txt");
InputStreamResource inputStreamResource = new InputStreamResource(new ByteArrayInputStream("Hello File".getBytes(StandardCharsets.UTF_8)));
return ResponseEntity.ok().headers(headers).body(inputStreamResource);
}
After calling http://{SERVER-ADDRESS}:{PORT}/file/get, a file with name test.txt that contains Hello File, geting download.
No comments:
Post a Comment