java项目测试或者不使用request,如何获取webroot路径

1.使用jdk中的方法,然后根据项目编译后的文件存在的位置,获取到classes目录,然后向上级查询获取
String path = EngineTest.class.getResource("/").toURI().getPath(); // /F:/eclipse2/workspace/项目/WebRoot/WEB-INF/classes/
String canonicalPath
= new File(path).getParentFile().getParentFile().getCanonicalPath(); // F:\eclipse2\workspace\项目\WebRoot
System.out.println(canonicalPath);
// F:\eclipse2\workspace\项目\WebRoot

WebRoot后边 \ 这个需要自己加
2.根据System类获取项目路径,然后在找WebRoot路径
Strng xiangmuPath = System.getProperty("user.dir"); // F:\eclipse2\workspace\项目
String webrootPath = xiangmuPath + "\WebRoot";

猜你喜欢

转载自www.cnblogs.com/renjianjun/p/10417189.html
今日推荐