部署在tomcat服务器上时,我想从项目文件夹后退一步的路径中读取文件。
例如:

项目文件夹:

apache-tomcat-7.0.63/webapps/Test/index.jsp


现在我想读取webapps文件夹中的文件

apache-tomcat-7.0.63/webapps/test.xml


使用

File inputFile = new File(getServletContext().getRealPath("test.xml"));


我该如何实施呢?

提前致谢。

最佳答案

您必须从基本上下文目录获取父目录路径:

File contextBasePath = new File(getServletContext().getRealPath(""));


然后将其与所需的文件名连接起来:

String fileName = "test.xml";
File inputFile = new File(contextBasePath.getParent() + '/' + fileName);

10-07 19:12
查看更多