如何在Java的Web项目中找到用户定义的配置文件(xml)的路径

时间:2012-03-15 05:33:00

标签: java web filepath

我在“webapp”下添加了一个文件,但是如何访问它呢? 服务器是tomcat和Spring框架作为框架。

提前致谢。

以下是一些代码和输出:

System.out.println("dddddddddddddddddddddddddddd");
//System.out.println(path.getPath());
InputStream is = getClass().getResourceAsStream("/WEB-INF/GroupWebService.xml");
if (null == is ){
    System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeee");
}

输出

log4j:WARN Please initialize the log4j system properly.
dddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeee
Mar 15, 2012 1:55:52 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'application'

2 个答案:

答案 0 :(得分:2)

您可以使用

 File directory = new File (".");
 String path = "";
 try {   
        path = directory.getAbsolutePath()
        System.out.println ("Current directory's absolute  path: "+ directory.getAbsolutePath());
 }catch(Exception e) {
     System.out.println("Exceptione is ="+e.getMessage());
 }

可能会打印项目目录的路径,然后您可以使用以下语句遍历您的WEB-INF

if(!path.equals("")){
  path += File.seperator + "WEB-INF" + File.seperator + "filename.xml";
}

答案 1 :(得分:1)

    System.out.println("dddddddddddddddddddddddddddd");
    //System.out.println(path.getPath());
    InputStream is = getClass().getResourceAsStream("/WEB-INF/util.tld");
    if (null == is ){
        System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeee");
    }else
    {
        System.out.println(is.getClass().getName());
    }