我正在研究struts2项目并遇到问题,以显示JasperReport Server在<object>
标记中生成的pdf报告。
以下是我正在使用的罐子:
以下是我的操作方法的代码段。
public String salesReportHtml() {
try {
...
for (int i = 0; i < tables.getTable("RS1").size(); i++) {
for (int j = 0; j < tables.getTable("RS2").get(i).size(); j++) {
System.out.println(j);
head.put(headerColumn.get(j), tables.getTable("RS1").get(i).get(headerColumn.get(j)));
}
}
for (int i = 0; i < tables.getTable("RS2").size(); i++) {
field = new HashMap<>();
for (int j = 0; j < tables.getTable("RS2").get(i).size(); j++) {
field.put(headerColumn.get(j), tables.getTable("RS2").get(i).get(headerColumn.get(j)));
}
datasourceList.add(field);
}
System.out.println("size datasourceList=>" + datasourceList);
baos =new ByteArrayOutputStream();
JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(datasourceList);
JasperPrint print = JasperFillManager.fillReport(
request.getServletContext().getRealPath("") + "/SCT/resources/newexcel.jasper", head, dataSource);
JasperExportManager.exportReportToPdfStream(print, baos);
reportStream=new ByteArrayInputStream(baos.toByteArray());
System.out.println("done");
return "success";
} catch (HibernateException e) {
e.printStackTrace();
return "error";
} catch (SQLException e) {
e.printStackTrace();
return "error";
}
catch (Exception e) {
printStatement.print("in exception");
e.printStackTrace();
return "error";
}
}
这是动作的struts配置。
<action name="salesReportPdf" class="com.mindcraft.sct.action.SalesReportAction"
method="salesReportPdf">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">reportStream</param>
<param name="contentDisposition">attachment;filename="sales.pdf" </param>
<param name="bufferSize">1024</param>
</result>
<result name="error">/SCT/jsp/ErrorJasper.jsp</result>
</action>
我已经对行动进行了ajax调用,并接收了收到的数据。
<html>
<body>
...
<div id=pdfDiv>
<object id=pdfview width="600" data="" type="application/pdf"></object>
</div>
</body>
</html>
function exportType() {
debugger;
var type = $('#reportid').val();
alert(type);
if (type == "pdf") {
alert("in pdf condition");
$.ajax({
url : "salesReportPdf.action",
async : false,
}).done(function(data) {
alert("success");
var pdf = data;
alert(pdf);
$('#pdfview').html('<object data="'+ pdf + '"/>');
//$('#pdfview').attr('data', pdf);
});
}
但是我在标签中收到了一个乱码数据。
#?? Fxw1?ߠ=? #^ 5ON N |?DF“IIII R' :m6nU&LT; VhYJ#= qxbj3&GT;ھcmOu | q C +&GT;JZͯQoPF/ 5)U#Wb的@ 0} M e+_s A R Yj;f ( k ^ vZ /( 3 / CJ+ gN -�ҭ����ջMl)?4q�\$�ņV˱P,��k�C�Ɛ�����\1�Y��٢��n�-��aY�XZ ���Ǻt�X!����``�:��0MNWs�n��Xj�g�d��y�+1�%�!5��|m!c��Njԓe��7� z�4�b�� �<ԋ�Ń4�@��%��c��n,�q:��>_���D�?V�ng�z[_��@�i�
b -c` &gt;]_O w o |NA 2 1 W66v C 4Jڍ \ &amp; o4 C* * aD?N {֡ ֡ 6 H 1- .eC :'] B {# Ԋj g{0 E h VI &gt; O F z # BEb$H1 _b4 p 1P I; 摢&gt; E(E
请帮我找到解决方案。