package com.gd.mvc.util;
import java.io.IOException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
/**
* 這是一個公共類 用於獲取web環境的路徑 名稱等信息
* @author lx
*
*/
public class GdWebPath {
private String webName;
private String webPhysicsPath;
private String systemName;
private String systemPath;
/**
* 該名稱返回的是web名稱,如:myApp
* @return String
*/
public String getWebName(){
return this.webName;
}
/**
* 該路徑指的是web環境的物理路徑,如:D:\eclipse\workspace\myApp
* @return
*/
public String getWebPhysicsPath(){
return this.webPhysicsPath;
}
public void getPath()throws IOException{
Class cls2=this.getClass();
ProtectionDomain pDomain=cls2.getProtectionDomain();
CodeSource cSource=pDomain.getCodeSource();
URL loc=cSource.getLocation();//file:/D:/eclipse/workspace/myApp/WEB-INF/classes/com/gd/web/webPath.class
webPhysicsPath=loc.toString().substring(loc.toString().indexOf("/")+1,loc.toString().indexOf("WEB-INF")-1);
System.out.println("webPhysicsPath "+webPhysicsPath);
int num=webPhysicsPath.lastIndexOf("/");
webName=webPhysicsPath.substring(num+1,webPhysicsPath.length());
System.out.println("webName: "+webName);
}
// public static void main(String args[]) throws IOException{
// GdWebPath gdw=new GdWebPath();
// gdw.getPath();
// }
}