• Now all data be ready for publish, We had made our blog alive now. Haha.

  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…

Downloading a file from spring controllers- Stack Overflow

Java PingBook 2 weeks ago (10-30) 8 0


I have a requirement where I need to download a PDF from the website. The PDF needs to be generated within the code, which I thought would be a combination of freemarker and a PDF generation framework like iText. Any better way?

However, my main problem is how do I allow the user to download a file through a Spring Controller?


@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());
} catch (IOException ex) {
log.info(“Error writing file to output stream. Filename was ‘{}'”, fileName, ex);
throw new RuntimeException(“IOError writing file to output stream”);


Generally speaking, when you have response.getOutputStream(), you can write anything there. You can pass this output stream as a place to put generated PDF to your generator. Also, if you know what file type you are sending, you can set


Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:Downloading a file from spring controllers- Stack Overflow
LIKE (0)
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website