为什么servlet无法获得jasper报告路径?

时间:2015-06-17 08:14:50

标签: java servlets jasper-reports filenotfoundexception

我是java中Web应用程序开发的新手,我正在使用net bean。

我创建了一个Web应用程序来在客户端打印jasper报告。为此,我使用了一个servlet。在这里我获得了上述问题。我以不同的方式给出了文件路径。但最后我必须将文件路径与当前工作目录(分区)放在一起。但我想用src文件夹的路径给它。

我也给了这个带有项目名称的路径。但我获得了以下例外。

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: Jasper\JasperReport\src\java\jasper\Report.jrxml (The system cannot find the path specified)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:222)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:193)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:184)
    at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:224)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:539)
    at jasper.Show.processRequest(Show.java:61)
    at jasper.Show.doGet(Show.java:113)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: Jasper\JasperReport\src\java\jasper\Report.jrxml (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:217)
    ... 28 more
Jasper\JasperReport\src\java\jasper\Report.jrxml
  1. 列表项

1 个答案:

答案 0 :(得分:0)

如果您粘贴了代码片段,那么帮助您会更容易。但无论如何我都会尝试。

方法JasperCompileManager.compileReportToFile将jrxml文件的路径作为参数,无论是absoulute还是相对于工作目录。而且我认为在这种情况下你不应该使用这种方法。

在你的情况下,我认为最好使用这样的东西:

InputStream jrxmlFileStream = getClass.getResourceAsStream("/path/to/file.jrxml");
// this path should be relative to src folder
JasperCompileManager.compileReportToStream(jrxmlFileStream, reportFileOutputStream);
// reportFileOutputStream - an OutputStream that writes to destination file

(我在这里省略了任何异常处理或资源关闭)