java get the root of the web project

Man of few words said, directly on the code:

public class Global {
 
  protected Logger log = LoggerFactory.getLogger(getClass());
  private static  String sysRootPath="";
  private static String classpath = "";
  static{
	  classpath = Global.class.getResource("/").getPath();
	  classpath = classpath.substring(1,classpath.length()-1);
		  if (!classpath.substring(0, 1)
	              .equals("/")
	         && !classpath.substring(1, 2)
	                     .equals(":")) {
			  classpath = "/" + classpath;
		  }
		  /**
		   * 系统根路径
		   */
		  sysRootPath = classpath;
			if(sysRootPath != null){
				File f = new File(sysRootPath);
				sysRootPath = f.getParentFile().getParent();
				sysRootPath = sysRootPath.replaceAll("\\\\","/");
			}
  }
  public static String getClassPath(){
	  return classpath;
  }
  
  public static String getSysRootPath(){
	  return sysRootPath;
  }

After the code is written as described above, directly Global.getSysRootPath () of the root system can be obtained

// the method described above to obtain a path space portion 20% occur, the following measures can display its normal

 path = URLDecoder.decode(path,"utf-8");

Guess you like

Origin blog.csdn.net/qq_41340666/article/details/80983798