
public static String getRealPath(Class clazz) {
String url = clazzgetResource("")getPath();
int displace = urlindexOf("WEB-INF");
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < displace - 1; i++) {
bufferappend(urlcharAt(i));
}
return buffertoString();
}
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:requestgetRequestURI()
结果:/TEST/testjsp
(2)得到工程名:requestgetContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:requestgetServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/testjsp
(4)得到页面所在服务器的全路径:applicationgetRealPath("页面jsp")
结果:D:/resin/webapps/TEST/testjsp
(5)得到页面所在服务器的绝对路径:absPath=new javaioFile(applicationgetRealPath(requestgetRequestURI()))getParent();
结果:D:/resin/webapps/TEST
2在类中取得路径:
(1)类的绝对路径:String u=ClassclassgetClass()getResource("/")getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:SystemgetProperty("userdir")
结果:D:/TEST
3在Servlet中取得路径:
(1)得到工程目录:requestgetSession()getServletContext()getRealPath("") 参数可具体到包名。
结果:E:/Tomcat/webapps/TEST
(2)得到IE地址栏地址:requestgetRequestURL()
结果:>
java获取根路径有两种方式:
1),在servlet可以用一下方法取得:
requestgetRealPath(“/”) 例如:filepach = requestgetRealPath(“/”) ”//upload//”;
2),不从jsp,或servlet中获取,只从普通java类中获取:
String path =
getClass()getProtectionDomain()getCodeSource()getLocation()getPath();
SAXReader() saxReader = new SAXReader();
if(pathindexOf(“WEB-INF”)>0){
path = pathsubstring(0,pathindexOf(“/WEB-INF/classes”) 16);
// ‘/WEB-INF/classes’为16位
document = saxReaderread(path filename);
}else{
document = saxReaderread(getClass()getResourceAsStream(filename));
}
weblogic tomcat 下都有效
String path =
getClass()getProtectionDomain()getCodeSource()getLocation()getPath();
<!--EndFragment-->
Java取得web工程目录->
第一步: 先获得classpath路径
String classpath = thisgetClass()getResource("/")getPath()replaceFirst("/", "");这样子可以得到classpath路径,类似于:
F:/projects/JavaStudyParent/study-springmvc-junit-test/target/springmvc-junit-test/WEB-INF/classes/
然后把WEB-INF/classes截取就能获得WebAPP目录啦:
String webappRoot = classpathreplaceAll("WEB-INF/classes/", "");得到的结果就是:
F:/projects/JavaStudyParent/study-springmvc-junit-test/target/springmvc-junit-test/
通过这个路径你就能获取该文件夹下的所有文件啦
java获取根路径有两种方式:
1)在servlet可以用一下方法取得:
requestgetRealPath(“/”)
例如:filepach = requestgetRealPath(“/”)+”//upload//”;
2)不从jsp,或servlet中获取,只从普通java类中获取:
String path = getClass()getProtectionDomain()getCodeSource()getLocation()getPath();
SAXReader() saxReader = new SAXReader();
if(pathindexOf(“WEB-INF”)>0){
path = pathsubstring(0,pathindexOf(“/WEB-INF/classes”)+16);
// ‘/WEB-INF/classes’为16位
document = saxReaderread(path+filename);
}else{
document = saxReaderread(getClass()getResourceAsStream(filename));
}
都瞎说什么玩应,web_inf是项目的信息目录,里面存着重要的配置文件等,如果可以直接访问,那你重要的文件不都暴露了吗,正常情况下web_inf里的东西是不能访问的,所以你要引用的不要放到img里
读取配置文件 , xxxproperties放在webroot/WEB-INF/classes/目录下
首先将配置文件转换成InputStream,有两种方式,原理一样,都是通过类加载器得到资源:
(1)InputStream inputStream = ThreadcurrentThread()getContextClassLoader()getResourceAsStream("xxproperties");
(2) InputStream inputStream =
thisgetClass() getClassLoader()getResourceAsStream( "xxproperties" );
调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,
而后在类类型上调用 getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父 子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就 是保证是和类类型同一个加载器加载的。
最后调用了类加载器的getResourceAsStream()方法来加载资源。
(3) 然后加载配置文件,读取属性值
Properties prop = new Properties();
propload(input);
String value = propgetProperty("PropertyName");
inputclose();
以上就是关于如何在java类中获取javaWeb的根路径全部的内容,包括:如何在java类中获取javaWeb的根路径、java 怎么获取web根目录、java如何获得linux下web路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)