Java 獲取webapp,Root,classpath,項目等路徑工具類
Java 獲取webapp,root,項目等路徑工具類
其實這個工具類,主要是利用了System.getProperty("user.dir")獲取你工程的絕對路徑。然后再做相關(guān)操作。
你在使用的過程中可以自己稍作修改。不多說了,直接上代碼。
ps:工具類可以在下面的附件里下載。
package com.sojson.common.utils;
import org.apache.commons.lang.StringUtils;
/**
*
* 靜態(tài)化路徑工具類
*
*
* www.sosjon.com
*
*
* 區(qū)分 責(zé)任人 日期 說明
* 創(chuàng)建 周柏成 2013-6-11
*
*
* @author
*
* @version 1.0, 2013-6-11
*
*/
public class UtilPath {
/**
* 獲取到classes目錄
* @return path
*/
public static String getClassPath(){
String systemName = System.getProperty("os.name");
//判斷當(dāng)前環(huán)境,如果是Windows 要截取路徑的第一個 '/'
if(!StringUtils.isBlank(systemName) && systemName.indexOf("Windows") !=-1){
return UtilPath.class.getResource("/").getFile().toString().substring(1);
}else{
return UtilPath.class.getResource("/").getFile().toString();
}
}
/**
* 獲取當(dāng)前對象的路徑
* @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/";
}
/**
* 文本換行,因為Linux 和 Windows 的換行符不一樣
* @return
*/
public static String nextLine(){
String nextLine = System.getProperty("line.separator");
return nextLine;
}
/**
* 獲取images 路徑
* @return
*/
public static String getImages(){
return getRootPath() + "images/" ;
}
/**
* 獲取sitemap 路徑
* @return
*/
public static String getSiteMapPath(){
return getRootPath() + "txt/sitemap" ;
}
/**
* 獲取Txt 路徑
* @return
*/
public static String getTxt(){
return getRootPath() + "txt" ;
}
}
自己可以稍作修改。
版權(quán)所屬:SO JSON在線解析
原文地址:http://suancuo.cn/blog/102.html
轉(zhuǎn)載時必須以鏈接形式注明原始出處及本聲明。
如果本文對你有幫助,那么請你贊助我,讓我更有激情的寫下去,幫助更多的人。
