導航:首頁 > 編程語言 > java物理路徑

java物理路徑

發布時間:2025-06-27 13:03:35

java 項目如何獲取項目所在的物理根路徑

在Java中獲取文件路徑是常見的需求,特別是在上傳文件操作中。對於Web應用,可以通過多種方式來獲取項目的物理根路徑。

在Web應用環境中,使用`this.getClass().getClassLoader().getResource("/").getPath()`和`this.getClass().getClassLoader().getResource("").getPath()`可以得到ClassPath的絕對URI路徑。例如:

`/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/`

而`this.getClass().getClassLoader().getResource(".").getPath()`則可以獲取項目的絕對路徑,例如:

`/D:/jboss-4.2.2.GA/server/default/deploy/hp.war`

另外,使用`this.getClass().getResource("/").getPath()`和`this.getClass().getResource("").getPath()`同樣可以得到當前類文件的URI目錄,如:

`/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/`

但`this.getClass().getResource(".").getPath()`在某些情況下可能無法運行。

在本地運行時,`Thread.currentThread().getContextClassLoader().getResource("/").getPath()`和`Thread.currentThread().getContextClassLoader().getResource("").getPath()`同樣可以得到ClassPath的絕對URI路徑,如:

`/D:/myProjects/hp/WebRoot/WEB-INF/classes/`

而`Thread.currentThread().getContextClassLoader().getResource(".").getPath()`則可以獲取項目的絕對路徑,如:

`/D:/myProjects/hp/WebRoot/WEB-INF/classes`

另外,`this.getClass().getResource("/").getPath()`和`this.getClass().getResource("").getPath()`可以得到當前類文件的URI目錄,如:

`/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/`

但`this.getClass().getResource(".").getPath()`在某些情況下可能無法運行。

最後,在Web應用程序中,通常使用`ServletContext.getRealPath("/")`方法來獲取Web應用程序根目錄的絕對路徑。此外,`request.getContextPath()`也可以獲取到上下文路徑。

但在Weblogic中,需要使用`request.getServletContext().getContextPath()`。然而,當將項目打包成war文件部署到Weblogic伺服器時,項目內部並沒有文件結構的概念,使用上述方式始終會返回null,無法獲取路徑。目前還沒有找到具體的解決方案。

Ⅱ Java web 怎麼實現下載啊,求助急急急急急!!

<%@pagelanguage="java"contentType="application/x-msdownload"pageEncoding="gb2312"%>
<%
//關於文件下載時採用文件流輸出的方式處理:
//加上response.reset(),並且所有的%>後面不要換行,包括最後一個;

response.reset();//可以加也可以不加
response.setContentType("application/x-download");

//application.getRealPath("/main/mvplayer/CapSetup.msi");獲取的物理路徑

Stringfiledownload="想辦法找到要提供下載的文件的物理路徑+文件名";
Stringfiledisplay="給用戶提供的下載文件名";
Stringfiledisplay=URLEncoder.encode(filedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename="+filedisplay);

java.io.OutputStreamoutp=null;
java.io.FileInputStreamin=null;
try
{
outp=response.getOutputStream();
in=newFileInputStream(filenamedownload);

byte[]b=newbyte[1024];
inti=0;

while((i=in.read(b))>0)
{
outp.write(b,0,i);
}
//
outp.flush();
//要加以下兩句話,否則會報錯
//java.lang.IllegalStateException:getOutputStream()hasalreadybeencalledfor//thisresponse
out.clear();
out=pageContext.pushBody();
}
catch(Exceptione)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in!=null)
{
in.close();
in=null;
}
//這里不能關閉
//if(outp!=null)
//{
//outp.close();
//outp=null;
//}
}
%>

閱讀全文

與java物理路徑相關的資料

熱點內容
北京黑馬程序員宿舍 瀏覽:945
光貓超級密碼為什麼加密 瀏覽:989
在中國人壽保險app如何復效 瀏覽:818
如何自學軟體開發程序員 瀏覽:313
ssh命令文件 瀏覽:117
禁用埠命令445 瀏覽:903
安卓如何設置微信黑版 瀏覽:857
命令含 瀏覽:709
新編日語2課後答案pdf 瀏覽:435
linux上下鍵 瀏覽:222
如何通過電腦升級app 瀏覽:67
phptcpsocket 瀏覽:71
蘋果百度網盤怎麼免費解壓文件 瀏覽:504
怎麼看伺服器php版本 瀏覽:603
怎麼寫一個pl0程序並編譯 瀏覽:405
電腦會話文件夾 瀏覽:235
如何在我的世界中添加伺服器 瀏覽:292
android流式標簽 瀏覽:896
h5誘導直播源碼 瀏覽:311
程序員那麼可愛男主什麼發現女主 瀏覽:761