Java 获取webapp,Root,classpath,项目等路径工具类
Java 获取webapp
,root
,项目等路径工具类
其实这个工具类,主要是利用了System.getProperty("user.dir")
获取你工程的绝对路径。然后再做相关操作。
你在使用的过程中可以自己稍作修改。不多说了,直接上代码。
ps:工具类可以在下面的附件里下载。
package com.sojson.common.utils;
import org.apache.commons.lang.StringUtils;
/**
*
* 静态化路径工具类
*
*
* www.sosjon.com
*
*
* 区分 责任人 日期 说明
* 创建 周柏成 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");
//判断当前环境,如果是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/";
}
/**
* 文本换行,因为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" ;
}
}
自己可以稍作修改。
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/102.html
转载时必须以链接形式注明原始出处及本声明。
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。