1: getPath() 获取相对路径
String path = clazz.getResource("").getPath();
return new File(path).getAbsolutePath();
2: getRootClassPath()
String path = PathKit.class.getClassLoader().getResource("").toURI().getPath();
return new File(path).getAbsolutePath();
3.getWebRootPath
String path = Class .class.getResource("/").toURI().getPath();
return new File(path).getParentFile().getParentFile().getCanonicalPath();
结果
E:\workspace\jfinal_demo_for_jsp\WebRoot\WEB-INF\classes\com\jfinal\kit
E:\workspace\jfinal_demo_for_jsp\WebRoot\WEB-INF\classes
E:\workspace\jfinal_demo_for_jsp\WebRoot
本文详细介绍了在Java Web应用中获取相对路径、根类路径和Web根路径的方法,包括使用Resource、Class类和PathKit类的相关API,并通过示例展示了获取路径的具体步骤和返回值解析。

1244

被折叠的 条评论
为什么被折叠?



