- //获取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){
- returnsysPath.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();
- intindex = 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;
- }
- }
来源: http://www.bubuko.com/infodetail-1947518.html