获取javaweb文件路径

1.java工具类中获取resources下面的文件夹下面的文件路径。例如resources/img/logo.png。

1
SendMailUtil.class.getClassLoader().getResource("img/logo.png").getPath()

输出结果:/D:/gitLocalRepo/innerManage/fmInnerManage/out/production/resources/img/logo.png

2.获取java工具类中classpath(class文件的路径)。

1
2
3
4
5
6
System.out.println(Class.class.getClass().getResource("/img/logo.png").getPath());
System.out.println(Thread.currentThread().getContextClassLoader().getResource("img/logo.png"));
System.out.println(SendMailUtil.class.getClassLoader().getResource("img/logo.png"));
System.out.println(ClassLoader.getSystemResource(""));
System.out.println(SendMailUtil.class.getResource(""));
System.out.println(SendMailUtil.class.getResource("/"));

/D:/gitLocalRepo/innerManage/fmInnerManage/out/production/resources/img/logo.png
file:/D:/gitLocalRepo/innerManage/fmInnerManage/out/production/resources/img/logo.png
file:/D:/gitLocalRepo/innerManage/fmInnerManage/out/production/resources/img/logo.png
file:/D:/gitLocalRepo/innerManage/fmInnerManage/out/production/classes/
file:/D:/gitLocalRepo/innerManage/fmInnerManage/out/production/classes/com/ynet/finmall/innermanage/utils/
file:/D:/gitLocalRepo/innerManage/fmInnerManage/out/production/classes/

刘俊重 wechat
欢迎关注我的微信公众号
坚持原创技术分享