当前位置: 代码迷 >> Java Web开发 >> 工程webroot下的xml文件路径如何写
  详细解决方案

工程webroot下的xml文件路径如何写

热度:5903   发布时间:2016-04-10 22:32:20.0
工程webroot下的xml文件路径怎么写?

如图,我想在我的一个class里引用webroot下的conf.xml文件,可以相对路径写不好,总是找不到,各位大神帮帮忙。。。

private static String filename = "C:\\conf.xml";//我现在是用的绝对路径,但是相对的应该怎么写?
File f = new File(filename);

------解决方案--------------------
引用:
Quote: 引用:

你这个是web项目的话,可以通过request.getRealPath("/config/conf.xml");来得到你WebRoot下config文件夹下的conf.xml文件。

在class里怎么用request啊?

ServletActionContext.getRequest()来得到HttpServletRequest:

HttpServletRequest request=ServletActionContext.getRequest();
String realPath =request.getRealPath("/");

这样就能得到你项目的服务器端路径了,也就是你tomcat下的webapps的路径