ITPub博客

首页 > IT职业 > IT职场 > java获取项目路径工具类

java获取项目路径工具类

IT职场 作者:duanhao 时间:2021-09-09 09:14:01 0 删除 编辑

public class UtilPath {

/**
 * 获取到classes目录
 * @return path
 */
public static String getClassPath(){
    String systemName = System.getProperty("os.name");//windows 10
    //判断当前环境,如果是Windows 要截取路径的第一个 '/'
    //indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1
    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("" + nextLine())
    .append(""+ nextLine())
    .append(""+ 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/" ;
}
public static void main(String []args)
{
    String url=UtilPath.getClassPath();
    System.out.println(url);
}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1343/viewspace-2808809/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-09-06

  • 博文量
    739
  • 访问量
    25594