FileInputStream和FileNotFound异常 [英] FileInputStream and FileNotFound Exception
问题描述
我正在尝试使用以下java代码检索相对路径中的jrxml文件:
I am trying to retrieve a jrxml file in a relative path using the following java code:
String jasperFileName = "/web/WEB-INF/reports/MemberOrderListReport.jrxml";
File report = new File(jasperFileName);
FileInputStream fis = new FileInputStream(report);
但是,很可能我没有成功定义相对路径并获得java.io. FileNotFoundException:执行期间出错。
However, most probably I didn't succeed in defining the relative path and get an java.io.FileNotFoundException: error during the execution.
由于我在Java I / O操作方面不是很有经验,所以我没有解决我的问题。欢迎任何帮助或想法。
Since I am not so experienced in Java I/O operations, I didn't solve my problem. Any helps or ideas are welcomed.
推荐答案
您正在尝试处理 jrxml
文件作为文件系统上的对象,但这不适用于Web应用程序。
You're trying to treat the jrxml
file as an object on the file-system, but that's not applicable inside a web application.
您不知道应用程序的部署方式和位置,所以你不能在它上面指一个文件
。
You don't know how or where your application will be deployed, so you can't point a File
at it.
而你想要使用 getResourceAsStream
来自 ServletContext
。类似于:
Instead you want to use getResourceAsStream
from the ServletContext
. Something like:
String resourceName = "/WEB-INF/reports/MemberOrderListReport.jrxml"
InputStream is = getServletContext().getResourceAsStream(resourceName);
就是你所追求的。
这篇关于FileInputStream和FileNotFound异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!