Summary of Java method for obtaining absolute path of Web project
•
Java
@H_419_1@ 一、用Jsp获取 1、获取文件的绝对路径 String file="文件";(例如:data.mdb) String path=application.getRealPath(file); 结果: E:\java_web\workspace\.Metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myWebsite\文件 2、获取文件的绝对路径 String p2=request.getRequestURI(); 结果: E:\java_web\workspace\.Metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myWebsite\文件 3、获取当前jsp页面的路径 String p3=request.getContextPath(); 结果: /myWebsite/index.jsp 4、获取当前项目的路径 String p4=request.getServletPath(); 结果: /myWebsite 二、用java类获取 1、获取Eclipse路径 String a1=System.getProperty("user.dir"); 结果: D:\StudySystem\JavaWeb\3-eclipse-jee-indigo-win32\eclipse 2、获取当前的classpath路径 String a2=类名.class.getResource("").toString(); String a3=DBConnection.class.getResource("/").toString(); String a4=DBConnection.class.getClassLoader().getResource("").toString(); String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); 打印出来分别是: file:/E:/java_web/workspace/.Metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myWebsite/WEB-INF/classes/com/site/db/ file:/E:/java_web/workspace/.Metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myWebsite/WEB-INF/classes/ file:/E:/java_web/workspace/.Metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myWebsite/WEB-INF/classes/ /E:/java_web/workspace/.Metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myWebsite/WEB-INF/classes/ 3、获取文件的绝对路径 如果要获取WebContent目录下的文件绝对路径怎么办?可以用下面的方法 String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); int num=t.indexOf(".Metadata"); String path=t.substring(1,num).replace('/','\\')+"项目名\\WebContent\\文件"; 结果是: E:\java_web\workspace\项目名\WebContent\文件 三、用servlet获取 1、获取项目的绝对路径 request.getSession().getServletContext().getRealPath("") 结果: E:\java_web\workspace\.Metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myWebsite 2、获取浏览器地址 request.getRequestURL() 结果: http://localhost:8080/myWebsite/QuestionServlet 3、获取当前文件的绝对路径 request.getSession().getServletContext().getRealPath(request.getRequestURI()) 结果: E:\java_web\workspace\.Metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myWebsite\myWebsite\QuestionServlet
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码