windows linux 下,获取java项目绝对路径的方法

struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题,

最好不要设置,使用struts默认

需要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)

public static String getRootPath() {
  String classPath = Tools.class.getClassLoader().getResource("/").getPath();
  String rootPath  = "";
  //windows下
  if("\\".equals(File.separator)){   
   rootPath  = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
   rootPath = rootPath.replace("/", "\\");
  }
  //linux下
  if("/".equals(File.separator)){   
   rootPath  = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
   rootPath = rootPath.replace("\\", "/");
  }
  return rootPath;
 }

原文地址:https://www.cnblogs.com/yangy608/p/2285817.html