随笔-26  评论-111  文章-19  trackbacks-0

 

 1    /**
 2     * 取得当前执行程序使用的classpath
 3     * @author snoics
 4     * @param theclass Class
 5     * @param path "" or "/"
 6     * @param encoding 如果encoding==null将默认使用utf-8进行解码
 7     * @return String
 8     */

 9    public static String getRealPath(Class theclass,String path,String encoding){
10     String realpath="";
11     if(encoding==null{
12      encoding="utf-8";
13     }
            //如果path==null则把它设置为""
14     path=getString(path);
15     if(path.equals("")) {
16      path="/";
17     }

18     String jarflag="";
19     String classflag="";
20     
21     String window_jarflag="jar:file:/";
22     String window_classfalg="file:/";
23     
24     String other_jarflag="jar:file:";
25     String other_classflag="file:";
26     
27     String endstring="!";
28     String systemtype="";
29     
30     String systemtype_windows="WINDOWS";
31     
32     String pachnamestringendstring="package ";
33     
34     String packagestring=theclass.getPackage().toString();
35     
36     packagestring=packagestring.substring(pachnamestringendstring.length());
37     packagestring=getSpecialReplaceString(packagestring,".","/");
38
39     String packagestringtemp=packagestring+"/";
40     
41     realpath=theclass.getResource(path).toString();
42     try{
43         realpath=URLDecoder.decode(realpath,encoding);
44     }
catch(Exception e){
45      e.printStackTrace() ;
46     }

47     
48     systemtype=System.getProperty("os.name").toUpperCase().trim();
49
50     if(systemtype.startsWith(systemtype_windows)){
51      jarflag=window_jarflag;
52      classflag=window_classfalg;
53     }
else{
54      jarflag=other_jarflag;
55      classflag=other_classflag;
56     }

57     
58     if(realpath.length()>=jarflag.length()){
59      String flagstring="";
60      flagstring=realpath.substring(0,jarflag.length());
61      if(!flagstring.equals(jarflag)){
62       flagstring=realpath.substring(0,classflag.length());
63      }

64      if(flagstring.equals(jarflag)){
65       realpath=realpath.substring(jarflag.length());
              //取得realpath中从开始到最后一个endstring字符串之前的
66       realpath=StringClass.getPreString(realpath,endstring);
              //取得realpath中从开始到最后一个"/"字符串之前的
67       realpath=StringClass.getPreString(realpath,"/")+"/";
68      }
else{
69       realpath=realpath.substring(classflag.length());
70       if(realpath.length()>packagestringtemp.length()) {
71           String subrelapath=realpath.substring(realpath.length()-packagestringtemp.length());
72           if(subrelapath.equals(packagestringtemp)){
                   //取得realpath中从开始到最后一个packagestring字符串之前的
73            realpath=StringClass.getPreString(realpath,packagestring);
74           }

75       }

76      }

77     }
else if((realpath.length()>=classflag.length())&&(realpath.length()<jarflag.length())){
78   realpath=realpath.substring(classflag.length());
79   if(realpath.length()>packagestringtemp.length()) {
80    String subrelapath=realpath.substring(realpath.length()-packagestringtemp.length());
81    if(subrelapath.equals(packagestringtemp)){
            //取得realpath中从开始到最后一个packagestring字符串之前的
82     realpath=StringClass.getPreString(realpath,packagestring);
83    }

84   }

85     }
            //返回经过格式化的路径,把"\"全部替换为"/"
86     return StringClass.getFormatPath(realpath);
87    }

88
posted on 2005-11-11 09:06 snoics 阅读(655) 评论(0)  编辑  收藏 所属分类: 学习 . 感悟

只有注册用户登录后才能发表评论。


网站导航: