hjg
2024-02-05 301115d5e96b56cd093eee3fcff2d60a15184162
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.io.UnsupportedEncodingException;
7 import java.net.URLEncoder;
8 import java.sql.Connection;
9 import java.sql.SQLException;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.sql.DataSource;
17
18 import org.apache.log4j.Logger;
19
20 import net.sf.jasperreports.engine.JRException;
21 import net.sf.jasperreports.engine.JasperFillManager;
22 import net.sf.jasperreports.engine.JasperPrint;
23 import net.sf.jasperreports.engine.JasperReport;
24 import net.sf.jasperreports.engine.JasperRunManager;
25 import net.sf.jasperreports.engine.export.JRPdfExporter;
26 import net.sf.jasperreports.engine.util.JRLoader;
27 import net.sf.jasperreports.export.SimpleExporterInput;
28 import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
29
30 /** 
31  * @author mengly 
32  * @version 创建时间:2015年10月6日 下午7:00:56 
33  * 类说明 
34  */
35
36 public class JasperMethod {
37     private static Logger log=Logger.getLogger(JasperMethod.class);
38     public static void writepdfbypost(HttpServletRequest request,HttpServletResponse response,String path,DataSource ds)
39     {
40         String jaspername=RequestParam.getString(request, "jaspername");
41         if(jaspername==null)
42             return;
43         jaspername=path+jaspername+".jasper";
44         Map<String, String[]> params=request.getParameterMap();
45         Map<String, Object> jasperparams=new HashMap<String, Object>();
46         if(request.getAttribute("jasperparams")!=null)
47             jasperparams=(Map<String, Object>)request.getAttribute("jasperparams");
48         for (Entry<String, String[]> entry : params.entrySet()) {
49             String key=entry.getKey();
50             String[] values=entry.getValue();
51             if(key.startsWith("jasper_")&&key.length()>7&&values.length>0)
52             {
53                 String param=key.substring(7);
54                 String value=values[0];
55                 jasperparams.put(param, value);
56             }
57         }
58         String filepath=request.getSession(true).getServletContext().getRealPath(jaspername);
59         File file=new File(filepath);
60         if(!file.exists())
61         {
62             log.info("file not exist");
63         }
64         log.info(file.getPath());
65         Connection con=null;
66         OutputStream out=null;
67         try {
68             JasperReport report=(JasperReport) JRLoader.loadObject(file);
69             con=ds.getConnection();
70             JasperPrint print=JasperFillManager.fillReport(report, jasperparams, con);
71             JRPdfExporter exporter=new JRPdfExporter();
72             exporter.setExporterInput(new SimpleExporterInput(print));
73             out= response.getOutputStream();
74             exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
75             String displayname="已经生产pdf报表.jasper";
76             displayname=URLEncoder.encode(displayname,"utf-8");
77             response.setHeader("Content-Disposition", "attachment; filename=\""+displayname+"\"");
78             response.setCharacterEncoding("utf-8");
79             response.setContentType("application/pdf");
80             exporter.exportReport();
81             out.flush();
82             out.close();
83         } catch (JRException e) {
84             e.printStackTrace();
85         } catch (UnsupportedEncodingException e) {
86             e.printStackTrace();
87         } catch (IOException e) {
88             e.printStackTrace();
89         } catch (SQLException e) {
90             e.printStackTrace();
91         }finally{
92             if(con!=null)
93                 try {
94                     con.close();
95                 } catch (SQLException e) {
96                     e.printStackTrace();
97                 }
98         }
99     }
100     
101     public static void writepdfbyfile(Map<String,Object> param,File f,HttpServletResponse response,DataSource ds)
102     {
103         Connection con=null;
104         try {
105             //JasperReport report=(JasperReport) JRLoader.loadObject(f);
106             con=ds.getConnection();
107             OutputStream out=response.getOutputStream();
108             byte[] bytes = JasperRunManager.runReportToPdf(f.getPath(), param, con);  
109             response.setContentType("application/pdf");
110             response.setContentLength(bytes.length);
111             String displayname=System.currentTimeMillis()+".pdf";
112             displayname=URLEncoder.encode(displayname,"utf-8");
113             response.setHeader("Content-Disposition", "inline; filename=\""+displayname+"\"");
114             response.setCharacterEncoding("utf-8");
115             response.setContentType("application/pdf");
116             out.write(bytes, 0, bytes.length);
117             out.flush();
118             out.close();
119         } catch (JRException e1) {
120             e1.printStackTrace();
121         } catch (SQLException e) {
122             e.printStackTrace();
123         } catch (IOException e) {
124             e.printStackTrace();
125         }finally{
126             if(con!=null)
127                 try {
128                     con.close();
129                 } catch (SQLException e) {
130                     e.printStackTrace();
131                 }
132         }
133     }
134 }