欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot获取Linux上项目的绝对路径和根目录

程序员文章站 2022-05-20 08:33:52
...
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
			String path2 = System.getProperty("user.dir");//参数即可获得项目相对路径。(ps:不知道是不是springboot内嵌tomcat容器的原因,用网上的request.getServletContext().getRealPath("/")方法获得的路径不是项目路径,而是c盘下一个tomcat目录路径
			try {
				String path1 = ResourceUtils.getURL("classpath:").getPath();
				System.out.println(path);
				System.out.println(path1);
				System.out.println(path2);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

输出结果

springboot获取Linux上项目的绝对路径和根目录

请选择合适方法使用