自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳細(xì)介紹Java相對(duì)路徑與絕對(duì)路徑的問題

開發(fā) 后端
本文介紹的是JAVA中的相對(duì)路徑與絕對(duì)路徑的一些問題,希望對(duì)大家有幫助,一起來看。

關(guān)于JAVA相對(duì)路徑絕對(duì)路徑的問題:

總結(jié)一下,遺漏的隨時(shí)補(bǔ)上

1.可以在servlet的init方法里 String path = getServletContext()。

getRealPath("/");

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

例如 :E:\eclipseM9\workspace\tree\ 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:\eclipseM9\workspace\tree\ tree是我web項(xiàng)目的根目錄

2 jsp 獲取文件路徑

  1. <%@ page contentType="text/html;   
  2. charset=gb2312" language="java" import="java.io.*" errorPage="" %>  
  3.  <html> <head>   
  4. <meta http-equiv="Content-Type"   
  5. content="text/html;   
  6. charset=gb2312">   
  7. <title>Untitled Document</title>  
  8. <head> <body>  

 

當(dāng)前WEB應(yīng)用的物理路徑:

  1. <%=application.getRealPath("/")%>  
  2. <BR> 當(dāng)前你求請(qǐng)的JSP文件的物理路徑:  
  3. <%=application.getRealPath(request.getRequestURI())%><BR>   
  4. <% String path=application.getRealPath(request.getRequestURI());   
  5. String dir=new File(path).getParent();   
  6. out.println("當(dāng)前JSP文件所在目錄的物理路徑"+dir);   
  7. %>   
  8. </body>   
  9. </html>   
  10. String virtPath = request.getServletPath();//虛擬路徑   
  11. 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)前路徑

  1. package org.cheng.file;   
  2. import Java.io.File;   
  3. public class FileTest {   
  4. public static void main(String[] args) throws Exception { 
  5. stem.out.println(Thread.currentThread().getContextClassLoader().getResource(""));   
  6. System.out.println(FileTest.class.getClassLoader().getResource(""));   
  7. System.out.println(ClassLoader.getSystemResource(""));   
  8. System.out.println(FileTest.class.getResource(""));   
  9. System.out.println(FileTest.class.getResource("/"));   
  10. //Class文件所在路徑 System.out.println(new File("/").getAbsolutePath());   
  11. System.out.println(System.getProperty("user.dir"));  
  12.  } }  

 

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

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

例如:如果你的weblogic安裝在c:\bea\weblogic700…… 那么,你的文件根路徑就是c:\. 所以,有兩種方式能夠讓你訪問你的服務(wù)器端的文件:

a.使用絕對(duì)路徑: 比如將你的參數(shù)文件放在c:\yourconfig\yourconf.properties, 直接使用 new FileInputStream("yourconfig/yourconf.properties");

b.使用相對(duì)路徑: 相對(duì)路徑的根目錄就是你的webapplication的根路徑,即WEB-INF的上一級(jí)目錄,將你的參數(shù)文件放 在yourwebapp\yourconfig\yourconf.properties, 這樣使用: new FileInputStream("./yourconfig/yourconf.properties"); 這兩種方式均可,自己選擇。

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

(3)。Resin 不是你的JSP放的相對(duì)路徑,是JSP引擎執(zhí)行這個(gè)JSP編譯成SERVLET 的路徑為根。比如用新建文件法測(cè)試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 默認(rèn)使用該方法的路徑是:WEB-INF/classes.已經(jīng)在Tomcat中測(cè)試。

希望通過本文的介紹,能給你帶來幫助。

責(zé)任編輯:于鐵 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-06-23 17:26:01

2011-06-23 15:33:24

SEO

2010-03-18 08:48:30

Python入門

2009-08-06 18:31:57

C#相對(duì)路徑絕對(duì)路徑

2022-11-16 10:13:29

Linux相對(duì)路徑絕對(duì)路徑

2022-08-29 14:11:10

Linux絕對(duì)路徑相對(duì)路徑

2021-07-29 20:28:24

靜態(tài)代碼Hdfs

2021-08-04 10:15:14

Go路徑語(yǔ)言

2022-11-04 10:49:56

Linux文件

2014-11-10 11:49:01

QCMSQCMS漏洞安全漏洞

2009-08-07 13:38:18

C#文件相對(duì)路徑

2009-08-07 13:24:35

C#獲取相對(duì)路徑

2022-04-24 15:33:38

鴻蒙開發(fā)操作系統(tǒng)

2015-03-30 15:45:41

javascripta標(biāo)簽絕對(duì)路徑方法

2009-08-27 10:40:56

Java路徑

2009-08-07 13:16:27

c#相對(duì)路徑寫法

2023-10-22 08:39:13

Linux目錄切換

2009-08-06 18:03:21

C#相對(duì)路徑

2019-08-30 09:54:39

LinuxC語(yǔ)言文本編輯器

2022-08-26 13:56:30

模塊JavaScript
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)