当前位置: 代码迷 >> Web前端 >> java取得web的根路径
  详细解决方案

java取得web的根路径

热度:98   发布时间:2012-10-08 19:54:56.0
java获得web的根路径

摘自:http://www.chentaoqian.com/archives/161

java获取根路径有两种方式:

1)servlet可以用一下方法取得:

request.getRealPath(“/”)?例如:filepach?=?request.getRealPath(“/”)+”//upload//”;

2),不从jsp,servlet中获取,只从普通java类中获取:

String?path?=?

????????getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

SAXReader()??saxReader?=?new?SAXReader();

?if(path.indexOf(WEB-INF)>0){

????path?=?path.substring(0,path.indexOf(/WEB-INF/classes)+16);

??????????????//??/WEB-INF/classes’为16

????document?=?saxReader.read(path+filename);

??}else{

???????document?=?saxReader.read(getClass().getResourceAsStream(filename));

?}

weblogic???tomcat?下都有效

String?path?=?

????getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

<!--EndFragment-->
  相关解决方案