Java 获取相关路径

假定你的web application 名称为news,你在浏览器中输入请求路径:

http://localhost:8080/news/main/list.jsp

则执行下面向行代码后打印出如下结果:

1、 System.out.println(request.getContextPath());   打印结果:/news
2、 System.out.println(request.getServletPath());   打印结果:  /main/list.jsp
3、 System.out.println(request.getRequestURI());    打印结果:/news/main/list.jsp
4、 System.out.println(request.getRealPath("/"));   打印结果: F:\Tomcat 6.0\webapps\news\test

 

 

路径问题
3.1 JSP中获得当前应用的相对路径和绝对路径
根目录所对应的绝对路径:request.getRequestURI()
文件的绝对路径  :application.getRealPath(request.getRequestURI());
当前web应用的绝对路径 :application.getRealPath("/");
取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()

 

3.2 Servlet中获得当前应用的相对路径和绝对路径
根目录所对应的绝对路径:request.getServletPath();
文件的绝对路径 :request.getSession().getServletContext().getRealPath
(request.getRequestURI())
当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");

(ServletContext对象获得几种方式:
javax.servlet.http.HttpSession.getServletContext()
javax.servlet.jsp.PageContext.getServletContext()
javax.servlet.ServletConfig.getServletContext()
)

 

 

 

 

对于普通java类文件,可以这样获取绝对路径:

System.out.println(this.getClass().getClassLoader().getResource("").getPath());
System.out.println(this.getClass().getClassLoader().getResource("/").getPath());
System.out.println(this.getClass().getResource("").getPath());
System.out.println(this.getClass().getResource("/").getPath());

在windows下面,输出为

/C:/Users/~/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp117/wtpwebapps/hims_agcy/WEB-INF/classes/
/C:/Users/~/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp117/wtpwebapps/hims_agcy/WEB-INF/classes/
/C:/Users/~/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp117/wtpwebapps/hims_agcy/WEB-INF/classes/bgi/health/agcy/birth/service/impl/
/C:/Users/~/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp117/wtpwebapps/hims_agcy/WEB-INF/classes/

在linux下面输出为:

/website/webApplications/HIMS_Agcy3/ROOT/WEB-INF/classes/
/website/webApplications/HIMS_Agcy3/ROOT/WEB-INF/classes/
/website/webApplications/HIMS_Agcy3/ROOT/WEB-INF/classes/bgi/health/agcy/birth/service/impl/
/website/webApplications/HIMS_Agcy3/ROOT/WEB-INF/classes/

 

String rootDir = this.getClass().getClassLoader().getResource("").getPath();

由上面的比较可以知道,对于windows需要去掉最前面的“/”;

String osName = System.getProperty("os.name");
  if(osName!=null && osName.toLowerCase().equals("window")){
   rootDir = rootDir.substring(1, rootDir.length());     // 对于windows环境下,去掉前面多余的斜杠
  }

 

ps:

PDFTmplConfiguration.class.getClassLoader().getResource("").getPath();

PDFTmplConfiguration.class.getClass().getResource("").getPath()

 虽然上面两种方法编译不报错,但是第二行的写法可能在运行期间报错,因为并没有加载class!!!

所以尽量选择classloader 来获取路径!!!

 

2015年3月12日16:25:57 添加:

今天在jar包中读取资源,又遇到一堆路径问题,jar包中读取文件更变态,参考这篇 http://blog.csdn.net/withiter/article/details/11924095     要打成jar的项目 如果需要访问jar里面的文件,

看起来只能读流

 

 ==============================================================================

 ==============================================================================

==============================================================================

InputStream in = WordUtil.class.getClassLoader().getResourceAsStream("template.docx");
System.out.println("in = "+in);
     
InputStream in2 = WordUtil.class.getResourceAsStream("template.docx");
System.out.println("in2 = "+in2);

 

String templateFile1 = WordUtil.class.getClassLoader().getResource("")+"template.docx";
System.out.println("templateFile = "+templateFile1);

System.out.println("path1 = "+WordUtil.class.getResource("/"));
System.out.println("path2 = "+WordUtil.class.getResource(""));

System.out.println("path3 = "+Thread.currentThread().getContextClassLoader().getResource(""));
System.out.println("path4 = "+WordUtil.class.getClassLoader().getResource(""));
System.out.println("path5 = "+ClassLoader.getSystemResource(""));
System.out.println("path6 = "+WordUtil.class.getResource(""));
System.out.println("path7 = "+WordUtil.class.getResource("/"));//Class文件所在路径
System.out.println("path8 = "+System.getProperty("user.dir"));


in = java.io.BufferedInputStream@7901880a
in2 = null
templateFile =F:/workspace/education-web/target/classes/template.docx


path1 = file:/F:/workspace/education-web/target/classes/
path2 = file:/F:/workspace/education-web/target/classes/com/education/web/util/
path3 = file:/F:/workspace/education-web/target/classes/
path4 = file:/F:/workspace/education-web/target/classes/
path5 = null
path6 = file:/F:/workspace/education-web/target/classes/com/education/web/util/
path7 = file:/F:/workspace/education-web/target/classes/
path8 = F:\workspace\education-web

 

 

你可能感兴趣的:(javaweb,Path,java os)