上代码:
/** * @param lsh * @param ywlxdm * @return * @throws Exception */ @RequestMapping(value = "/viewPdf", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public ResponseEntity<byte[]> viewPdf(String lsh, String ywlxdm) throws Exception { byte[] content = fpfwCenterService.viewPdf(lsh, ywlxdm); ResponseEntity<byte[]> response = null; HttpHeaders headers = new HttpHeaders(); //这行代码比较重要,有这个属性则告知浏览器下载文件,无则使用浏览器打开PDF headers.setContentDispositionFormData("name", "filename"); headers.setContentType(MediaType.parseMediaType("application/pdf")); headers.setCacheControl("must-revalidate, post-check=0, pre-check=0"); response = new ResponseEntity<byte[]>(content, headers, HttpStatus.OK); return response; }
byte[] content = fpfwCenterService.viewPdf(lsh, ywlxdm);
content为读取到的PDF二进制数组,以上即可实现PDF浏览器端的相关操作。