我想通过spring-mvc-rest控制器返回生成的pdf文件.这是我目前正在使用的代码的缩短版本:
@RestController @RequestMapping("/x") public class XController { @RequestMapping(value = "/", method = RequestMethod.GET) public ResponseEntityfind() throws IOException { byte[] pdf = createPdf(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType("application", "pdf")); headers.setContentDispositionFormData("attachment", "x.pdf"); headers.setContentLength(pdf.length); return new ResponseEntity (pdf, headers, HttpStatus.OK); } }
这工作得很好,它只是返回实际的字节数组作为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 [...]
(顺便说一下,响应甚至不包含结束"
:)
任何提示赞赏!