Downloading a file from spring controllers

前端 未结 14 892
渐次进展
渐次进展 2020-11-22 01:06

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

14条回答
  •  感情败类
    2020-11-22 01:36

    You should be able to write the file on the response directly. Something like

    response.setContentType("application/pdf");      
    response.setHeader("Content-Disposition", "attachment; filename=\"somefile.pdf\""); 
    

    and then write the file as a binary stream on response.getOutputStream(). Remember to do response.flush() at the end and that should do it.

提交回复
热议问题