java rootpath_Java 获取webapp,Root,classpath,项目等路径工具类

本文介绍了一个Java工具类UtilPath,用于获取不同环境下的webapp、Root、classpath、项目路径以及相关的目录路径,如HTML、FTL、WEB-INF、images、sitemap和TXT等。
摘要由CSDN通过智能技术生成

public class UtilPath {

public static void main(String[] args) {

String systemName = System.getProperty("os.name");

System.out.println(getProjectPath());

}

/**

* 获取到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" ;

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值