我想通过spring-mvc-rest控制器返回生成的pdf文件.这是我目前正在使用的代码的缩短版本:
@RestController
@RequestMapping("/x")
public class XController {
@RequestMapping(value = "/",method = RequestMethod.GET)
public ResponseEntity403_7@
这工作得很好,它只是返回实际的字节数组作为base64编码:(
curl -i 'http://127.0.0.1:8080/app/x'
Server: Apache-Coyote/1.1
Content-Disposition: form-data; name="attachment"; filename=x.pdf"
Content-Type: application/pdf
Content-Length: 138654
Date: Fri,08 Jan 2016 11:25:38 GMT
"JVBERi0xLjYNJeLjz9MNCjMyNCAwIG9iag [...]
@H_403_7@
(顺便说一句,响应甚至不包含结束“:)
任何提示赞赏!
最佳答案
我使用你的代码创建了这个例子,但是一个非常类似的方法是在我的web应用程序中完成他的工作:
原文链接:https://www.f2er.com/spring/432801.html@RequestMapping(value = "/",method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response,HttpServletRequest request) throws IOException
{
byte[] pdf = createPdf();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment; filename=foo.pdf");
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.getOutputStream().write(pdf);
}
@H_403_7@
否则你可以尝试这个答案Open ResponseEntity PDF in new browser tab