我正在尝试在浏览器中显示pdf。但它无法正常显示错误“无法加载pdf文档”。我们使用Spring来显示PDF。这是我显示pdf的代码。任何人都可以帮我解决这个问题。
public <T> ResponseEntity<byte[]> generateReportByJavaList(List<T> listValues) {
this.jasperPrint = null;
try {
File temp;
if (StringUtils.isNotBlank(pdfFileName)) {
temp = new File(pdfFileName);
} else {
temp = File.createTempFile(this.tempPrefix, ".pdf");
}
this.jasperPrint = JasperFillManager.fillReport(this.reportFile, this.params,
new JRBeanCollectionDataSource(listValues, false));
JasperExportManager.exportReportToPdfFile(jasperPrint, temp.getAbsolutePath());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
Path p = Paths.get(temp.getAbsolutePath());
String filename = p.getFileName().toString();
headers.add("content-disposition", "inline;filename=" + filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response1 = new ResponseEntity<byte[]>( headers, HttpStatus.OK);
return response1;
} catch (IOException e) {
System.out.println(e + "generateReportByJavaList :" + e.getMessage());
} catch (JRException e) {
System.out.println(e + "generateReportByJavaList :" + e.getMessage());
}
return null;
}