我想从网站下载PDF。 PDF需要在代码中生成,我认为这将是freemarker和像iText这样的PDF生成框架的组合。有更好的方法吗?
但是,主要问题是,我不知道如何允许用户通过Spring Controller下载文件?
答案 0 :(得分:2)
@RequestMapping(value = "/downlaod/{fileName}", method = RequestMethod.GET)
public void getFile(
@PathVariable("fileName") String fileName,
HttpServletResponse response) {
try {
InputStream is = ...; // get uploaded file using InputStream
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream()); // copy this to response's OutputStream
response.flushBuffer();
} catch (IOException ex) {
log.info("Error writing file to output stream. Filename was '{}'", fileName, ex);
throw new RuntimeException("IOError to writing file on output stream");
}
}
如果你有response.getOutputStream(),你可以在那里写下你想要的东西。您已将此输出流作为放置生成的PDF的位置传递。你也可以设置
response.setContentType("应用/ PDF&#34);