@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
public void getFile(
@PathVariable("file_name") String fileName,
HttpServletResponse response) {
try {
// get your file as InputStream
InputStream is = ...;
// copy it to response's OutputStream
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
} catch (IOException ex) {
log.info("Error writing file to output stream. Filename was '{}'", fileName, ex);
throw new RuntimeException("IOError writing file to output stream");
}
}
Well, you can write anything using response.getOutputStream(). Using this you can pass an output stream as a place to put generated PDF to your generator. Also, in case, you know what file type you are sending, you can write:
response.setContentType("application/pdf");
Hope this helps!
Get your Spring Certification today to become certified.