用ireport做报表的经验

20151127:
晚上下了本电子书,ireport开发详解,按他说的,官网下载了jaspereport-lib后,把disk目录下jar都拷贝到lib下,靠,不停的报错,根据报错依次在ireport下找到了对应的jar,commons-logging-1.1.jar,commons-collections-3.2.1.jar,commons-digester-2.1.jar,groovy-all-2.0.1.jar,iText-2.1.7.js2.jar,虽然不知道有些jar包是干嘛的,加上disk下的jasperreports-5.6.0.jar,jasperreports-applet-5.6.0.jar,jasperreports-fonts-5.6.0.jar,jasperreports-javaflow-5.6.0.jar,commons-beanutils-1.8.2.jar,以及之前打包的仿宋字体simfang.jar,完整可以出来了。这书里起码要说下要依赖哪些包啊!
20151130:
ServletContext有个getRealPath(String name)
this.getClass().getClassLoader().getResource(""),ClassLoader有个getResource("")
第一个取真实路径然后加上name,第二个是取资源,会找,找不到就返回null,
ClassLoader的getResourceAsStream(String name)就不用说了
String realPath = this.getServletContext().getRealPath("");
System.out.println(" realPath:"+realPath);
String realPath2 = this.getServletContext().getRealPath("/");
System.out.println(" realPath2:"+realPath2);
realPath2 = this.getServletContext().getRealPath("\\");
System.out.println(" realPath2:"+realPath2);
String realPath4 = this.getServletContext().getRealPath("/aaa/bbb");
System.out.println("realPath4:"+realPath4);
//		this.getServletContext().getResourcePaths(arg0)
//		this.getServletContext().getResourceAsStream(arg0)
//		this.getServletContext().getResource(arg0)
String realPath3 = this.getClass().getClassLoader().getResource("").getPath();
System.out.println("realPath3=" + realPath3);
realPath3 = this.getClass().getClassLoader().getResource("rebel.xml").getPath();
System.out.println("realPath3=" + realPath3);
//      this.getClass().getClassLoader().getResourceAsStream(name)
//		this.getClass().getClassLoader().getResources(name)
输出:
 realPath:D:\edpywcw\rpttest\WebRoot
 realPath2:D:\edpywcw\rpttest\WebRoot\
 realPath2:D:\edpywcw\rpttest\WebRoot\
realPath4:D:\edpywcw\rpttest\WebRoot\aaa\bbb
realPath3=/D:/edpywcw/rpttest/WebRoot/WEB-INF/classes/
realPath3=/D:/edpywcw/rpttest/WebRoot/WEB-INF/classes/rebel.xml


两个都有getResource()  getResourceAsStream getResources/getResoucePaths方法


JasperRunManager.runReportToPdfStream(getServletContext().getResourceAsStream("/report11.jasper"), sos, parameters);

这句时,开始写成

FileInputStream fis =  new FileInputStream(getServletContext().getRealPath("/report11.jasper"));
JasperRunManager.runReportToPdfStream(fis, sos, parameters);
fis.close();

然后想起ServletContext的getResourceAsStream,改成:

JasperRunManager.runReportToPdfStream(getServletContext().getResourceAsStream(getServletContext().getRealPath("/report11.jasper")), sos, parameters);

报错,因为getResourceAsStream是相对于上下文路径而言,不适用于绝对路径,最后:

JasperRunManager.runReportToPdfStream(getServletContext().getResourceAsStream("/report11.jasper"), sos, parameters);


你可能感兴趣的:(用ireport做报表的经验)