springboot获取 static文件夹的绝对路径,适用于内置targert或部署外部tomcat

 public static  String  getUrl() {

	 String path = null;
	 try {
		 String serverpath= ResourceUtils.getURL("classpath:static").getPath().replace("%20"," ").replace('/', '\\');
		  path=serverpath.substring(1);//从路径字符串中取出工程路径
	 } catch (FileNotFoundException e) {
		 e.printStackTrace();
	 }
	 return path;
 }

 String serverpath= ResourceUtils.getURL("classpath:static").getPath() 获取static文件夹绝对路径

replace("%20"," ").replace('/', '\\'); 字符空格替换及 / 转换为 \\ 
          path=serverpath.substring(1);// 此时获取到的路径前面带一个多余的符号通过字符串解决即可获取实际的绝对路径

获取路径后  getUrl()+"\\libs\\a.dll" 方式拼接处理

猜你喜欢

转载自blog.csdn.net/wuyufeng891021/article/details/84614727