Если вы:
byte[]
перед отправкой в ответ; InputStream
; @ControllerAdvice
для исключения. Ниже приведен код, который вам нужен:
@RequestMapping(value = "/stuff/{stuffId}", method = RequestMethod.GET)
public ResponseEntity downloadStuff(@PathVariable int stuffId)
throws IOException {
String fullPath = stuffService.figureOutFileNameFor(stuffId);
File file = new File(fullPath);
HttpHeaders respHeaders = new HttpHeaders();
respHeaders.setContentType("application/pdf");
respHeaders.setContentLength(12345678);
respHeaders.setContentDispositionFormData("attachment", "fileNameIwant.pdf");
InputStreamResource isr = new InputStreamResource(new FileInputStream(file));
return new ResponseEntity(isr, respHeaders, HttpStatus.OK);
}
Также обратите внимание, что, чтобы избежать прочтения всего файла только для вычисления его длины, лучше сохранить его ранее. Убедитесь, что вы проверяете документы для InputStreamResource
.