应用根路径整理类

import java.io.File;

/**
 * @author SiPei.Qiao
 * @function 应用根路径整理类
 */
public class PathUtil {
    /**
     * @function 得到类所在的本地目录
     * @return
     */
    public String getWebClassesPath() {
        String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        return path;
    }

    /**
     * 得到WEB-INF根目录
     * 
     * @return
     * @throws IllegalAccessException
     */
    public String getWebInfPath() throws IllegalAccessException {
        String path = getWebClassesPath();
        if (path.indexOf("WEB-INF") > 0) {
            if ("\\".equals(File.separator)) {
                path = path.substring(0, path.indexOf("WEB-INF") + 8);
                path = path.substring(1, path.length());
                // path = path.replaceAll("/", "\\");
                // System.out.println(path);
            } else {
                path = path.substring(0, path.indexOf("WEB-INF") + 8);
            }
        } else {
            throw new IllegalAccessException("Can't get WebInfo path");
        }
        return path;
    }

    /**
     * @function 得到应用的根目录
     * @return
     * @throws IllegalAccessException
     */
    public String getWebRoot() throws IllegalAccessException {
        String path = getWebClassesPath();
        if (path.indexOf("WEB-INF") > 0) {
            String keyWord = "WEB-INF";
            if ("\\".equals(File.separator)) {
                path = path.substring(1, path.indexOf(keyWord));
            } else {
                path = path.substring(0, path.indexOf(keyWord));
            }
        } else {
            throw new IllegalAccessException("Can't get WebRoot path");
        }
        return path;
    }

    /**
     * @function 得到应用的根目录
     * @return
     */
    public String getWebRootEx() {
        try {
            return getWebRoot();
        } catch (Exception e) {
            return null;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值