我正在尝试构建一个示例 Web 应用程序,它加载通过 jasper 报告(.jasper 文件)生成的 pdf 报告。但是,该报告不会以 pdf 格式打开并显示消息“此 PDF 文档可能无法正确显示”。代码片段如下。服务器日志中没有异常日志。任何帮助都会很棒。谢谢。
Servlet 控制器类
@WebServlet("/generateReport")
public class JasperReportsExample extends HttpServlet{
final String deviceJasper = "SampleReport.jasper";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");
try{
List<SampleReportBean> sampleReportBeanList = new ArrayList<SampleReportBean>();
SampleReportBean bean = new SampleReportBean();
bean.setName("Hello");
sampleReportBeanList.add(bean);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(deviceJasper);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(sampleReportBeanList);
Map<String, Object> parameters = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanCollectionDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, deviceJasper);
}catch(Exception e) {
System.out.println("Error:while creating jasper report....");
e.printStackTrace();
}
}
}
html页面
<form action="/samplejasperreport/generateReport" method="get">
<div>
<input type="submit" name="Generate Report" value="Generate Report">
</div>
</form>
目录结构图
错误画面:
元芳怎么了
相关分类