0
点赞
收藏
分享

微信扫一扫

jasper 生成 PDF 代码

ArrayList<UserInfo> arrayList=new ArrayList<UserInfo>();
UserInfo userInfo;
for(int i=0;i<100;i++){
userInfo=new UserInfo();
userInfo.setName("name"+"00"+i);
userInfo.setAge(i);
arrayList.add(userInfo);
}
String jrxmlName="ceShi";
String fileName= request.getSession().getServletContext().getRealPath("")+"/WEB-INF/reports/" + jrxmlName + ".jasper";
JRBeanCollectionDataSource jr = new JRBeanCollectionDataSource(arrayList);
Hashtable<String, Object> parameters=new Hashtable<String, Object>();
parameters.put("time", "20090808");
JasperPrint print;
try {
print = JasperFillManager.fillReport(fileName,parameters,jr);
String pdfFullPath="D://あああ.pdf";
JasperExportManager.exportReportToPdfFile(print,pdfFullPath);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

 

此代码为在服务器上生成PDF,可以再写个下载PDF的代码片段 ,如下:

 

response.setContentType("application/octet-stream;charset=shift_jis");
response.setHeader("Content-Disposition", "attachment; filename=/""
+ new String(pdfFullPath.getBytes(), "iso-8859-1") + "/"");
InputStream in = new FileInputStream(new File("D://あああ.pdf"));
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int i = -1;
while ((i = in.read(buffer)) != -1) {
out.write(buffer, 0, i);
}
out.flush();
in.close();
out.close();
out=null;
in=null;

举报

相关推荐

0 条评论