java 项目的路径详情

title: 项目下的路径问题
tags:
grammar_cjkRuby: true
---

在javaee的项目中,存取文件,解析xml和properties文件,以及项目中的文件,都需要获取路径,常用的一些路径收集如下:

一、src文件夹下的文件,编译后 的classes文件夹

1.直接获取src文件夹下的文件的方法如下:(同样路径可以是“com/huawei/db.properties”)

   InputStream is = DemoFilePath.class.getClassLoader().getResourceAsStream("db.properties");
   

其中的DemoFilePath为类名,而且只能实用类名,不能实用Object。获取src文件下的文件的输入流都可以通过这个方法来获取
还有一种方法和原理是一样的,可以实用类名或者Object超类也是通过类加载器来获取只是前面需要增加-- “/” --这个符号如下:(同样路径可以是"/com/huawei/test.txt")

InputStream is=Object.class.getResourceAsStream("/test.txt");//保险的做法还是把Object换成类本身的名字
InputStream is = getClass().getResourceAsStream("/db2.properties");//但是这个方法不能子啊静态类中使用

2.获取classpath的路径,就是src编译后了classes文件夹的路径

DemoFilePath.class.getClassLoader().getResource("com/renboqieqie/db4.properties").getPath()
//或者
Thread.currentThread().getContextClassLoader().getResource("").getPath()

二、WebConten文件夹下

1.在jsp中获取

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>1.JSP</title>
</head>
<body>
<h1>获取文件的绝对路径</h1>
<h2>application.getRealPath(file)</h2>
<%=application.getRealPath("contact.xml") %><br/>
<%=application.getRealPath("db.properties") %><br/>
<%=application.getRealPath("index.html") %><br/>

<hr/>
<h1>获取文件的绝对路径</h1>
<h2>request.getRequestURI()</h2>
<%=request.getRequestURI() %>
<hr/>
<h1>获取当前jsp页面的路径</h1>
<h2>request.getContextPath()</h2>
<%=request.getContextPath() %>
<hr/>
<h1>获取当前项目的路径</h1>
<h2>request.getServletPath()</h2>
<%=request.getServletPath() %>
<hr/>
</body>
</html>

jsp中输出后的表现效果
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



不积跬步,无以至千里,不积小流,无以成江海! 实践则生,空谈则死!
posted @ 2017-02-17 13:37  KING磊  阅读(350)  评论(0编辑  收藏  举报