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