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();
|
}
|
}
|
}
|
}
|