@RequestMapping(value = "/example", produces = "application/json; charset=utf-8")
public @ResponseBody
void example(@RequestParam(value = "url") String url, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws SQLException {
try {
File reportFile;
reportFile = new File(request.getSession().getServletContext().getRealPath("example.jasper"));
JasperHelper.printReport("pdf", reportFile, parameters, this.dataSourceMNG.getConnection(), response);
} catch (Exception ex) {
Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);
}
File reportFile = new File("");
try{
reportFile = new File(request.getSession().getServletContext().getRealPath("example.jasper"));
JasperHelper.printReport("pdf", reportFile, parameters, this.dataSourceMNG.getConnection(), response);
}catch(Exception ex){
Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void printReport(String exportType, JasperReport jasperReport, String fileName, Map parameters, JRDataSource dataSource, HttpServletResponse response) {
try {
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
fileName = fileName.substring(0, fileName.length() - 6) + exportType;
response.setContentType("application/" + exportType);
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
if (exportType.equalsIgnoreCase("pdf")) {
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
exporter.exportReport();
} else if (exportType.equalsIgnoreCase("xls")) {
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
exporter.exportReport();
} else if (exportType.equalsIgnoreCase("xlsx")) {
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
exporter.exportReport();
} else if (exportType.equalsIgnoreCase("rtf")) {
JRRtfExporter exporter = new JRRtfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleWriterExporterOutput(response.getOutputStream()));
exporter.exportReport();
}
response.flushBuffer();
} catch (Exception ex) {
Logger.getLogger(JasperHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}
抱歉我的英语不好。我点击了一个按钮,当我点击按钮时会执行示例方法。它将调用printReport两次并导出到一个pdf文件。但是当打开pdf文件有3页时,我在微软边缘打开它显示第一页,当它在chrome上打开它显示2结束页面。我不知道为什么会出现问题?如何解决它? 感谢。