hikamen
2/25/2018 - 10:21 AM

Java Web获取Web应用根路径

//1
String path = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");

//2
public void contextInitialized(ServletContextEvent sce) {
    String path = sce.getServletContext().getRealPath("/");
}

//3 在Web项目中获取classes的路径
this.getClass().getResource("/").getPath()