用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);

這句時,開始寫成 java

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

而後想起ServletContext的getResourceAsStream,改爲: app

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

報錯,由於getResourceAsStream是相對於上下文路徑而言,不適用於絕對路徑,最後: 字體

JasperRunManager.runReportToPdfStream(getServletContext().getResourceAsStream("/report11.jasper"), sos, parameters);
相關文章
相關標籤/搜索