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)输出:
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);