我正在尝试构建一个示例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>
SampleReport.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="SampleReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="038b20ef-43f5-4f05-8318-627e403a7110">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBean Adapter"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band splitType="Stretch"/>
</title>
<pageHeader>
<band splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band splitType="Stretch"/>
</columnHeader>
<detail>
<band height="31" splitType="Stretch">
<textField>
<reportElement x="150" y="0" width="140" height="30" isPrintWhenDetailOverflows="true" uuid="f6eec303-affe-4804-bb6a-e5c07516a46f"/>
<textElement textAlignment="Center">
<font size="22"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="1" splitType="Stretch"/>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
</jasperReport>
目录结构图片
错误屏幕:
答案 0 :(得分:0)
感谢AlexK:
JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());//send the pdfstream to the browser