ireport输出到pdf

使了个小把戏,把它分成了两步,一步用来读出并保存blob图像成一个本地文件,一步用来将图片导入到pdf文件中。
源代码如下
读出并保存:
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>

<% try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@gisserver:1521:yl","test","test");
String id=request.getParameter("id");
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from TEST.livestock_pic WHERE ID="+id;
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
InputStream in = rs.getBinaryStream("PIC");

String saveFile =  "car1.jpg" ;
File myFile = new File(saveFile);
FileOutputStream sos =new FileOutputStream(myFile);

int len = 0;
byte[] b = new byte[1024];

while((len = in.read(b)) > 0){
sos.write(b,0,len);}
in.close();
sos.flush();
rs.close();
stmt.close();
conn.close();}}
catch(Exception e) {
       }            
%>
//跳转到显示页
<%response.sendRedirect("xianshi.jsp");%>


显示:
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="net.sf.jasperreports.engine.export.*"%>
<%@ page import="net.sf.jasperreports.engine.util.*"%>
<%@ page import="net.sf.jasperreports.engine.JasperExportManager"%>
<%@ page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>

<%
request.setCharacterEncoding("GB2312");
String it=request.getParameter("id");

//报表编译之后生成的.jasper文件存放位置
File reportFile = new File(application.getRealPath("/report/tx.jasper"));

//连接数据库
 String url="jdbc:oracle:thin:@192.168.6.222:1521:yl";

 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

 Map parameters= new HashMap();
 String myname ="car1.jpg";
 parameters.put("path",myname);


 System.out.println("-------conn------");

 

//连接到数据库

Connection conn = DriverManager.getConnection(url,"test","test");

 System.out.println(reportFile.getPath());
 System.out.println("----Japer begin-----");

 byte[] bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,conn);

 System.out.println("------Jasper end------");

 response.setContentType("application/pdf");
 response.setContentLength(bytes.length);
 
 ServletOutputStream ouputStream = response.getOutputStream();

 ouputStream.write(bytes,0,bytes.length);

 ouputStream.flush();

 ouputStream.close();

 
%>

当然,大家在定制报表的时候要注意定义一个叫path的参数,并将它的值赋给你所插入图像的expresion value

我在我的机器上运行是通过了的,win2000+tomcat5.016,看到大家都为这个恼火,我就发个帖子与大家共享,希望大家共同学习jasperreport,有问题大家共同商讨。
阅读更多
个人分类: 好文收藏
上一篇jboss+eclipse
下一篇写给自己看的,理解J2EE,总结自己的学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭