日期:2014-05-17  浏览次数:20752 次

工程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的路径