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、獲取文件的絕對路徑
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