- 浏览: 118108 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (131)
- J2EE (5)
- 系统配置 (2)
- eclipse (3)
- MySQL (3)
- 数据库 (1)
- log4j (2)
- hibernate (5)
- JBOSS (3)
- https (2)
- Struts2 (7)
- javascript (2)
- CSS (4)
- HTTP (2)
- J2SE (1)
- apache (6)
- XML (1)
- android (3)
- session (3)
- Spring (3)
- 前端优化 (2)
- Drools (2)
- java笔试题 (8)
- SVN (2)
- linux (19)
- linux命令 (3)
- memcache (1)
- Servlet (2)
- Java RMI (1)
- 领域模型 (1)
- LVS (4)
- Linux shell (2)
- WEB (1)
- F5 (2)
- WinRAR (1)
- TCP (1)
- MQ (2)
- weblogic (1)
- winxp安装FTP (1)
- WebLogic和Tomcat (1)
- 虚拟机 (1)
- nexus-maven (1)
- jstl (1)
- jvm (1)
- Redis (2)
- mongodb (3)
- JAVA (2)
- mybatis (1)
- SQLserver (1)
- GC (1)
- zabbix (1)
- node (1)
最新评论
-
huosange:
简单明了,通俗易懂
JAVA为什么只允许单继承
(2)先生成pdf文件,然后通过strut2下载:
获取登录用户以及链接地址:
获取文件,这个必须有:
// 从下载文件原始存放路径读取得到文件输出流
ReportUtil.java
值得注意的是:对于ie7及以下版本的浏览器必须设置浏览器相应头的缓存设置:
网上有解释:This is a typical MSIE error message when a download is been provided over HTTPS (SSL) while the response headers are been set to disable the browser cache
添加的代码:
对于火狐浏览器,有可能文件名过长,而背截取,导致文件类型错误,无法下载打开等,可以作如下设置:
struts2配置文件:
对于中文文件名的处理:
struts2的一些配置也可以直接在java类里面设置:
还需要注意:如果项目发布在linux上,需注意,windows上的“/”对应linux上的“\”,但是windows上这个不用区别开来,都可以识别路径
response.setHeader("Cache-Control", "public"); response.setHeader("Pragma", "public"); //response.setHeader("Content-Disposition", "attachment; filename=" + docName+".pdf"); response.setContentType("application/pdf"); String reportBasePath = ServletActionContext.getServletContext().getRealPath(DOWNLOADFILEPATH); //System.out.println("reportBasePath....is : "+reportBasePath); String template = reportBasePath+"/"+"convenientVisa.jasper"; //System.out.println("template....is : "+template); String reportOutputBasePath = ServletActionContext.getServletContext().getRealPath(DOWNLOADFILEPATH); String pdfCn = ReportUtil.createReportName(pdfNameCn,reportType); String pdfEn = ReportUtil.createReportName(pdfNameEn,reportType); String outputFileCn = reportOutputBasePath +"/"+ pdfCn; String outputFileEn = reportOutputBasePath +"/"+ pdfEn; //System.out.println("outputFileCn....is : "+outputFileCn); //System.out.println("outputFileEn....is : "+outputFileEn); if(!new File(template).exists()){ //System.out.println("报表模版不存在"); throw new Exception("报表模版不存在"); } File reportOutputBaseFile = new File(reportBasePath + reportOutputBasePath); if(!reportOutputBaseFile.exists())reportOutputBaseFile.mkdirs(); //确定要导出的文件的名称 if(url.indexOf("/cn/")>-1){ ReportUtil.exportReports(ffpTravelDetailList, reportParameter,ReportUtil.REPORT_FORMAT_PDF,template, outputFileCn); }else{ ReportUtil.exportReports(ffpTravelDetailList, reportParameter,ReportUtil.REPORT_FORMAT_PDF,template, outputFileEn); } //InputStream in = ServletActionContext.getServletContext().getResourceAsStream(outputFile); public boolean regex(String regex, String str) { Pattern p = Pattern.compile(regex, Pattern.MULTILINE); Matcher m = p.matcher(str); return m.find(); }
获取登录用户以及链接地址:
LpAccount user=(LpAccount) ActionContext.getContext().getSession().get("user");//获取登录用户名 String url = ServletActionContext.getRequest().getRequestURL().toString();//获取响应的链接地址
获取文件,这个必须有:
// 从下载文件原始存放路径读取得到文件输出流
public InputStream getDownloadFile() { String url = ServletActionContext.getRequest().getRequestURL().toString(); if(url.indexOf("/cn/")>-1){ return ServletActionContext.getServletContext().getResourceAsStream( DOWNLOADFILEPATH + pdfNameCn+".pdf"); }else{ return ServletActionContext.getServletContext().getResourceAsStream( DOWNLOADFILEPATH + pdfNameEn+".pdf"); } }
ReportUtil.java
package com.csair.skypearl.utils; import java.io.PrintWriter; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.export.JRHtmlExporter; import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter; import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; import net.sf.jasperreports.j2ee.servlets.ImageServlet; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.struts2.ServletActionContext; public class ReportUtil { public static final String REPORT_FORMAT_XLS = "XLS"; public static final String REPORT_FORMAT_XLSX = "XLSX"; public static final String REPORT_FORMAT_DOC = "DOC"; public static final String REPORT_FORMAT_PDF = "PDF"; public static final String REPORT_FORMAT_HTML = "HTML"; /** * <B>导出报表</B><br /> * create by:vakin * at:2011-1-7 * @param <T> * @param reportDatas * @param parameters * @param exportType * @param template * @param outputFile * @throws Exception */ @SuppressWarnings("unchecked") public static <T> void exportReports(List<T> reportDatas, Map parameters,String exportType, String template, String outputFile) throws Exception { //HttpServletRequest request = ServletActionContext.getRequest(); JasperReport jasperreport = DynamicColumnReport.dynamicColumnReport( null, template); //根据参数判断是否需要动态生成列 JasperPrint print = JasperFillManager.fillReport(jasperreport,parameters,new JRBeanCollectionDataSource(reportDatas)); JRExporter exporter = null; if(REPORT_FORMAT_XLS.equalsIgnoreCase(exportType)){ exporter = new JRXlsExporter(); }else if(REPORT_FORMAT_DOC.equalsIgnoreCase(exportType)){ exporter = new JRDocxExporter(); }else if(REPORT_FORMAT_PDF.equalsIgnoreCase(exportType)){ exporter = new JRPdfExporter(); }else if(REPORT_FORMAT_HTML.equalsIgnoreCase(exportType)){ exporter = new JRHtmlExporter(); }else if(REPORT_FORMAT_XLSX.equalsIgnoreCase(exportType)){ exporter = new JRXlsxExporter(); } exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFile); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.exportReport(); } /** * <B>输出报表图</B><br /> * create by:vakin * at:2011-1-7 * @param <T> * @param reportDatas * @param parameters * @param template * @throws Exception */ @SuppressWarnings("unchecked") public static <T> void createReportChart(List<T> reportDatas, Map parameters, String template) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); PrintWriter printWriter = response.getWriter(); JasperPrint jasperPrint = JasperFillManager.fillReport(template, new HashMap(), new JRBeanCollectionDataSource(reportDatas)); JRHtmlExporter htmlExporter = new JRHtmlExporter(); response.setContentType("text/html"); ServletActionContext.getRequest().getSession().setAttribute( ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, printWriter); htmlExporter.setParameter( JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8"); String imageUrl = "reportChart?__r="+ System.currentTimeMillis() +"&image="; htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageUrl); htmlExporter.exportReport(); printWriter.flush(); printWriter.close(); } public static String getReportBasePath(){ return ServletActionContext.getServletContext().getRealPath("/jasreportTemplete/"); } public static String createReportName(String modelName,String docType){ return modelName + "."+docType.toLowerCase(); } }
值得注意的是:对于ie7及以下版本的浏览器必须设置浏览器相应头的缓存设置:
网上有解释:This is a typical MSIE error message when a download is been provided over HTTPS (SSL) while the response headers are been set to disable the browser cache
添加的代码:
response.setHeader("Cache-Control", "public"); response.setHeader("Pragma", "public");
对于火狐浏览器,有可能文件名过长,而背截取,导致文件类型错误,无法下载打开等,可以作如下设置:
String browser = this.getRequest().getHeader("User-Agent"); if(browser != null && browser.indexOf("MSIE") == -1) { if(!(url.indexOf("/cn/")>-1)){ docmNameEn = "=?UTF-8?B?" + (new String (Base64.encodeBase64(docmNameEn.getBytes("UTF-8")))) + "?="; } }
struts2配置文件:
<param name="fileName">${docNameEn}.pdf</param> <result name="download" type="stream"> <!-- 下载文件类型定义 --> <param name="contentType">application/pdf</param> <!-- 下载文件处理方法 --> <param name="contentDisposition"> attachment;filename="${docNameEn}.pdf" </param> <!-- 下载文件输出流定义 --> <param name="inputName">downloadFile</param> </result>
对于中文文件名的处理:
String docmName=new String(new String(pdfNameCn).getBytes("GBK"),"ISO8859-1"); String docmNameEn = new String(new String(pdfNameEn).getBytes("GBK"),"ISO8859-1");
struts2的一些配置也可以直接在java类里面设置:
response.setHeader("Content-Disposition", "attachment; filename=" + docName+".pdf");//[color=red]attachment表示下载,inline表示在线查看[/color] response.setContentType("application/pdf");//文件格式设置,具体可百度ContentType
还需要注意:如果项目发布在linux上,需注意,windows上的“/”对应linux上的“\”,但是windows上这个不用区别开来,都可以识别路径
发表评论
-
struts2实例
2013-04-12 11:24 688Struts2使用拦截器完成权限控制示例 文章分类:Java编 ... -
Struts1与Struts2的特点与比较
2013-04-12 11:17 988Struts1与Struts2的特点与 ... -
struts2整合jasper模版生成PDF导出下载<三>
2012-08-18 14:34 1446三,编写java程序,生成pdf文件 此处有多钟方式:也可以 ... -
struts2整合jasper模版生成PDF导出下载<二>
2012-08-18 14:24 1043jasperreport模版制作过程可参见: http://w ... -
struts2整合jasper模版生成PDF导出下载<一>
2012-08-18 14:14 1290被这个问题困扰了好几天,今天终于解决。现在我将呈现如何用Str ... -
struts2弹出下载页面
2012-07-30 15:25 748有时候配置struts2的action时:少了<para ...
相关推荐
36<br>i18n: 36<br>Resource Bundle Base name 36<br>XML源文件的编码设置: 37<br>5 报表元素 37<br>选择并插入元素到报表中: 37<br>布置和元素顺序 40<br>使用元素树管理元素: 43<br>基本属性: 43<br>线 46...
36<br>i18n: 36<br>Resource Bundle Base name 36<br>XML源文件的编码设置: 37<br>5 报表元素 37<br>选择并插入元素到报表中: 37<br>布置和元素顺序 40<br>使用元素树管理元素: 43<br>基本属性: 43<br>线 46...
" width="800" height="600" ><span>打印pdf(old)</span> </a></li> <li><a class="icon" href="hj02Action!rpt_pdfold" target="dwzExport" targetType="dialog" title="开始打印?" width="800" height...
修改pom文件加入两个坐标 jstl标签库 和 jasper <project xmlns=...
通过ireport生成模版,使用jasperreport调用模版,通过servlet生成pdf格式的文件,显示于页面以实现本地打印功能
Jersey 插件在 Jersey 模板系统中集成 Jasper Report,生成 PDF 文件。 安装 安装它有两个步骤: 在 pom.xml 中包含相关性; 注册 jersey-mvc-jasper 的 MvcFeature; 包含依赖 <dependency> <groupId>br.uff....
上传个免费的,文档加了图了。之前那个下载下来是有点儿问题,里面没图。
Java Project for reporting using jasper report + struts2
JspTest是一个不依赖予容器,对jsp页面进行单元测试的junit扩展。内部它使用Jasper作为jsp编译引擎... <artifactId>jsptest-jsp20</artifactId> <version>0.21</version> </dependency> 标签:JspTest
<br>查了很多资料,都没有解决,有说在Tomcat/common/lib中加入ant.jar,删除原来jasper-compiler-jdt.jar,修改web.xml,我试了多次,在JBuilder2006里始终没有解决。<br><br>后来记得自己另外安装过一个Tomcat...
1. *.jrxml 文件配置 <textElement><font fontName="宋体" /></textElement> 2.pom.xml中配置 <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports-fonts</artifactId> <version>...
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param> <param-name>classdebuginfo</param-name> <param-value>true</param-value> </init-param> <load-on-startup>3</load-on...
JasperReports插件 该专家将Jasper文件编译到目标目录。 :waving_hand: 该插件即将接受 我已经多年没有使用过这个插件了,所以我想把这个项目交给真正使用它的人。 如果您有兴趣,请提出一个问题与... < version>2.
使用ireort 工具生成jasper文件然后,导出excel,pdf,html<br>文件的例子
asm-2.2.jar commons-collections-2.1.1.jar commons-digester-1.7.jar commons-fileupload-1.2.1.jar commons-logging-1.0.4....struts2-jasperreports-plugin-2.1.8.1.jar xwork-core-2.1.6.jar iTextAsianCmaps.jar
ireport+jasper中文手册 pdf版
jasper外国软件但是免费就要用,导出中文费劲,网上介绍很多升级iText包也没好使后,找的对于初学者来说很开心解决办法,zip里附加了字体文件和导出后的jar
maven struts2 jasperreport ireport亲测通过整合多家资源
jasper ireport 生成 报表,导入直接运行,不必修改任何配置
<result name="result_jasper" type="jasper"> <param name="location">${jasperLocation}</param> <param name="format">${jasperFormat}</param> <param name="dataSource">jasperDataSource</param> ...