當(dāng)前位置:首頁文章首頁 IT學(xué)院 Java

java、struts、tomcat程序中的各種取路徑的方法

作者:  來源:  發(fā)布時(shí)間:2011-12-9 10:56:22  點(diǎn)擊:

我和一向?qū)Τ绦虮容^感興趣,但因?yàn)楹芏嘣驔]有能堅(jiān)持下來,不過也收集了一些有用的代碼吧.

得到當(dāng)前路徑
File f = new File(“.”);
String absolutePath = f.getAbsolutePath();
System.out.println(absolutePath);

1.路徑中空格:

String pathtem=null;
URL ut=Thread.currentThread().getContextClassLoader().getResource(“”);
pathtem=ut.toString();
pathtem=pathtem.replace(“%20″, ” “);//去掉空格
pathtem=pathtem.substring(6,pathtem.length());//去掉”file:/”
返回:
d:/apache/webapp/Ev/WEB-INF/classes/

struts中得到路徑
String rootuploadpath = this.getServlet().getServletContext().getRealPath(“/”);

2.生成jar包后
String pathtem=null;
URL ut=Csssb_se.class.getResource(“”);
pathtem=ut.toString();
pathtem=pathtem.replace(“%20″, ” “);
pathtem=pathtem.substring(0,pathtem.indexOf(projectname));
pathtem=pathtem.replace(“file:///”,”");
pathtem=pathtem.replace(“file:/”,”");
pathtem=pathtem.replace(“jar:”,”");

3.生成war包后,Tomcat

String Dirpath = null;
String Project_name = “TranData”;
URL ut = Thread.currentThread().getContextClassLoader().getResource(“”);
Dirpath = ut.toString();
Dirpath = Dirpath.replace(“%20″, ” “);
Dirpath = Dirpath.substring(6, Dirpath.length());
int project_name_index = Dirpath.indexOf(Project_name);
Dirpath = Dirpath.substring(0, project_name_index
+ Project_name.length() + 1);

String xmlfilepath = Dirpath + “s.xml”;

文章評論

軟件按字母排列: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z