Java相對(duì)路徑與絕對(duì)路徑的問題
來源:易賢網(wǎng) 閱讀:920 次 日期:2014-09-05 16:18:39
溫馨提示:易賢網(wǎng)小編為您整理了“Java相對(duì)路徑與絕對(duì)路徑的問題”,方便廣大網(wǎng)友查閱!

在寫java程序時(shí)不可避免要獲取文件的路徑…總結(jié)一下,遺漏的隨時(shí)補(bǔ)上

1.可以在servlet的init方法里

String path = getServletContext()。getRealPath("/");

這將獲取web項(xiàng)目的全路徑

例如 :E:eclipseM9workspacetree

tree是我web項(xiàng)目的根目錄

2.你也可以隨時(shí)在任意的class里調(diào)用

this.getClass()。getClassLoader()。getResource("/")。getPath();

這將獲取 到classes目錄的全路徑

例如 : E:eclipseM9/workspace/tree/WEB-INF/classes/

這個(gè)方法也可以不在web環(huán)境里確定路徑,比較好用

3.request.getContextPath();

獲得web根的上下文環(huán)境

如 /tree

tree是我的web項(xiàng)目的root context

1. 可以在servlet的init方法里

String path = getServletContext()。getRealPath("/");

這將獲取web項(xiàng)目的全路徑

例如 :E:eclipseM9workspacetree

tree是我web項(xiàng)目的根目錄

2

jsp 獲取文件路徑

2008-08-06 16:57

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>Untitled Document</title>

</head>

<body>

當(dāng)前WEB應(yīng)用的物理路徑:<%=application.getRealPath("/")%><BR>

當(dāng)前你求請(qǐng)的JSP文件的物理路徑:<%=application.getRealPath(request.getRequestURI())%><BR>

<%

String path=application.getRealPath(request.getRequestURI());

String dir=new File(path)。getParent();

out.println("當(dāng)前JSP文件所在目錄的物理路徑"+dir);

%>

</body>

</html>

String virtPath = request.getServletPath();//虛擬路徑

String realPath = request.getRealPath(virtPath);//物理路徑

JSP中獲得當(dāng)前應(yīng)用的相對(duì)路徑和絕對(duì)路徑

根目錄所對(duì)應(yīng)的絕對(duì)路徑:request.getRequestURI()

文件的絕對(duì)路徑 :application.getRealPath(request.getRequestURI());

當(dāng)前web應(yīng)用的絕對(duì)路徑 :application.getRealPath("/");

取得請(qǐng)求文件的上層目錄:new File(application.getRealPath(request.getRequestURI()))。getParent()

Servlet中獲得當(dāng)前應(yīng)用的相對(duì)路徑和絕對(duì)路徑

根目錄所對(duì)應(yīng)的絕對(duì)路徑:request.getServletPath();

文件的絕對(duì)路徑 :request.getSession()。getServletContext()。getRealPath

(request.getRequestURI())

當(dāng)前web應(yīng)用的絕對(duì)路徑 :servletConfig.getServletContext()。getRealPath("/");

(ServletContext對(duì)象獲得幾種方式:

Javax.servlet.http.HttpSession.getServletContext()

Javax.servlet.jsp.PageContext.getServletContext()

Javax.servlet.ServletConfig.getServletContext()

Java 的Class中獲得相對(duì)路徑,絕對(duì)路徑的方法

單獨(dú)的Java類中獲得絕對(duì)路徑

根據(jù)Java.io.File的Doc文擋,可知:

默認(rèn)情況下new File("/")代表的目錄為:System.getProperty("user.dir")。

一下程序獲得執(zhí)行類的當(dāng)前路徑

package org.cheng.file;

import Java.io.File;

public class FileTest {

public static void main(String[] args) throws Exception {

System.out.println(Thread.currentThread()。getContextClassLoader()。getResource(""));

System.out.println(FileTest.class.getClassLoader()。getResource(""));

System.out.println(ClassLoader.getSystemResource(""));

System.out.println(FileTest.class.getResource(""));

System.out.println(FileTest.class.getResource("/")); //Class文件所在路徑

System.out.println(new File("/")。getAbsolutePath());

System.out.println(System.getProperty("user.dir"));

}

}

服務(wù)器中的Java類獲得當(dāng)前路徑(來自網(wǎng)絡(luò))

(1)。Weblogic

WebApplication的系統(tǒng)文件根目錄是你的weblogic安裝所在根目錄。

例如:如果你的weblogic安裝在c:beaweblogic700……

那么,你的文件根路徑就是c:.

所以,有兩種方式能夠讓你訪問你的服務(wù)器端的文件:

a.使用絕對(duì)路徑:

比如將你的參數(shù)文件放在c:yourconfigyourconf.properties,

直接使用 new FileInputStream("yourconfig/yourconf.properties");

b.使用相對(duì)路徑:

相對(duì)路徑的根目錄就是你的webapplication的根路徑,即WEB-INF的上一級(jí)目錄,將你的參數(shù)文件放

在yourwebappyourconfigyourconf.properties,

這樣使用:

new FileInputStream("./yourconfig/yourconf.properties");

這兩種方式均可,自己選擇。

(2)。Tomcat

在類中輸出System.getProperty("user.dir");顯示的是%Tomcat_Home%/bin

(3)。Resin

不是你的JSP放的相對(duì)路徑,是JSP引擎執(zhí)行這個(gè)JSP編譯成SERVLET

的路徑為根。比如用新建文件法測試File f = new File("a.htm");

這個(gè)a.htm在resin的安裝目錄下

(4)。如何讀相對(duì)路徑哪?

在Java文件中g(shù)etResource或getResourceAsStream均可

例:getClass()。getResourceAsStream(filePath);//filePath可以是"/filename",這里的/代表web

發(fā)布根路徑下WEB-INF/classes

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:Java相對(duì)路徑與絕對(duì)路徑的問題
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)