首页 > 编程语言 > 详细

java获取web项目根路径

时间:2017-02-17 00:18:47      阅读:313      评论:0      收藏:0      [点我收藏+]
//获取web 项目根路径
public static String getWebPath() {
        String sysPath = "";
        try {
            //SfgkUtil 为该方法所在类名称
            if(SfgkUtil.class.getClassLoader().getResource(File.separator)==null){
                return getOtherWebPath();
            }
            sysPath = SfgkUtil.class.getClassLoader().getResource(File.separator).getPath();
            sysPath = URLDecoder.decode(sysPath, "UTF-8");
            if(sysPath.indexOf("WEB-INF")!=-1){
                return sysPath.substring(0, sysPath.indexOf("WEB-INF"));
            }
            else{    
                return getOtherWebPath();
            }    
        } catch (Exception e) {
            e.printStackTrace();
            return sysPath;
        }
    }

    private static String getOtherWebPath(){
        String sysPath = "";
        URL url = SfgkUtil.class.getProtectionDomain().getCodeSource().getLocation();   
        String path = url.toString();   
        int index = path.indexOf("WEB-INF");
        try {
            path = URLDecoder.decode(path, "UTF-8");
            if(index!=-1){
                sysPath =path.substring(0, index);
                sysPath =sysPath.replace("file:/", "");
                return sysPath;
            }
            else{
                sysPath =path.replace("file:/", "");
                return sysPath;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return sysPath;
        }
       
    }

java获取web项目根路径

原文:http://www.cnblogs.com/nycqw/p/6407686.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!