我正在使用Struts2和IDE Eclipse。我有一个chart.xml
文件,放在WEB-INF文件夹中,我试图将此文件读作:
String path=ServletActionContext.getServletContext().getRealPath("/WEB_INF/chart.xml");
File XMLfile = new File(path);
但无法阅读。我收到以下错误:
javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.FileNotFoundException: F:\doc\Mavenex\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\EFW11\WEB-INF\chart1.xm (The system cannot find the file specified)]
我该怎么做?
答案 0 :(得分:1)
ServletContext.java有两个可用于操作资源的API。不使用File
类来获取文件,而是使用这些API,它们对您要执行的操作非常有用。它们是getResourceAsStream(java.lang.String path)
,返回java.io.InputStream and
getResource(java.lang.String path)which returns a
java.net.URL`。
答案 1 :(得分:0)
WEB-INF是项目结构的私有目录,只能由Web服务器访问。你无法从WEB-INF目录中读取文件。将您的文件放在其他文件夹中并尝试阅读它。