我正在尝试使用Spring MVC Controller下载PDF文件(请参阅下面的代码)。 但是,尽管使用了“内容处理”,PDF文件仍显示在浏览器中(Chrome / Firefox / Internet Explorer)。相反,我想要显示“另存为”对话框。有没有人看到我的代码有什么问题,或者我是否需要额外的弹簧配置才能使用?
@Controller
public class PdfController {
@Autowired
private PdfService pdfService;
@RequestMapping(value = "/" + "temp", method = RequestMethod.GET)
public void downloadPdf(final HttpServletRequest request,
final HttpServletResponse response) throws IOException,
TemplateException, DocumentException {
InputStream pdfInputStream = new ByteArrayInputStream(
pdfService.createPdf());
IOUtils.copy(pdfInputStream, response.getOutputStream());
String filename = "CustomFileName.pdf";
response.setHeader("Content-Disposition", "attachment; filename="
+ filename.replace(" ", "_"));
response.setContentType("application/pdf");
response.flushBuffer();
}
}