import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
/**
*
* 靜態化路徑工具類
*/
public class UtilPath {
/**
* 獲取到classes目錄
* @return path
*/
public static String getClassPath(){
String systemName = System.getProperty("os.name");
//判斷當前環境,如果是Windows 要截取路徑的第一個 '/'
if(!StringUtils.isBlank(systemName) && systemName.indexOf("Windows") !=-1){
return UtilPath.class.getResource("/").getFile().toString().substring(1);
}else{
return UtilPath.class.getResource("/").getFile().toString();
}
}
/**
* 獲取當前對象的路徑
* @param object
* @return path
*/
public static String getObjectPath(Object object){
return object.getClass().getResource(".").getFile().toString();
}
/**
* 獲取到項目的路徑
* @return path
*/
public static String getProjectPath(){
return System.getProperty("user.dir");
}
/**
* 獲取 root目錄
* @return path
*/
public static String getRootPath(){
return getWEB_INF().replace("WEB-INF/", "");
}
/**
* 獲取輸出HTML目錄
* @return
*/
public static String getHTMLPath(){
return getFreePath() + "html/html/";
}
/**
* 獲取輸出FTL目錄
* @return
*/
public static String getFTLPath(){
return getFreePath() + "html/ftl/";
}
/**
* 獲取 web-inf目錄
* @return path
*/
public static String getWEB_INF(){
return getClassPath().replace("classes/", "");
}
/**
* 獲取模版文件夾路徑
* @return path
*/
public static String getFreePath(){
return getWEB_INF() + "ftl/";
}
/**
* 獲取一個目錄下所有的文件
* @param path
* @return
*/
public static File[] getFiles(String path){
File file = new File(path);
File[] files = file.listFiles();
return files;
}
/**
* 獲取當前時間 + 中國時區
* @return
*/
public static String getDate(){
SimpleDateFormat sformart=new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String result = sformart.format(new Date());
result = result.replace("_", "T");
result += "+08:00";
return result;
}
/**
* 不帶結尾的XmlSitemap頭部
* @return
*/
public static String getXmlSitemap(){
StringBuffer sb = new StringBuffer()
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + nextLine())
.append("<?xml-stylesheet type=\"text/xsl\" href=\"sitemap.xsl\"?>"+ nextLine())
.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"+ nextLine());
return sb.toString();
}
/**
* 文本換行
* @return
*/
public static String nextLine(){
String nextLine = System.getProperty("line.separator");
return nextLine;
}
/**
* 獲取domain
* @param request
* @return
*/
public static String getDomain(HttpServletRequest request) {
return ((String) request.getSession().getAttribute("nowPath")).replaceAll("(www.)|(.com)|(.net)|(http://)", "").trim();
}
/**
* 獲取images 路徑
* @return
*/
public static String getImages(){
return getRootPath() + "images/" ;
}
}
java項目路徑的獲取--靜態化路徑工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.