从Eclipse中的Web-Inf文件夹中读取文件



我正在使用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:docMavenex.metadata.pluginsorg.eclipse.wst.server.coretmp1wtpwebappsEFW11WEB-INFchart1.xm (The system cannot find the file specified)]

我该怎么做?

servletContext.java有两个API,您可以用来操纵资源。它们没有使用File类获取文件,而是使用这些API,而是对您想做的事情非常有用。它们是 getResourceAsStream(java.lang.String path),它返回 java.io.InputStream and getResource(java.lang.string路径) which returns a java.net.url`。

Web-Inf是项目结构的私有目录,仅通过Web服务器访问。您无法从Web-Inf目录读取文件。将您的文件放入其他文件夹中,然后尝试阅读它。

最新更新