當前位置:首頁 » 編程語言 » java獲取伺服器路徑

java獲取伺服器路徑

發布時間: 2023-09-24 14:32:34

1. java 怎麼獲取伺服器webroot的路徑

使用JAVA後台代碼取得WEBROOT物理路徑,可以有如下兩種方式:
1、使用JSP Servlet取得WEB根路徑可以用request.getContextPath(),相對路徑request.getSession().getServletContext().getRealPath("/"),它們可以使用我們很容易取得根路徑。

2、如果使用了spring, 在WEB-INF/web.xml中,創建一個webAppRootKey的param,指定一個值(默認為webapp.root)作為鍵值,然後通過Listener,或者Filter,或者Servlet執行String webAppRootKey = getServletContext().getRealPath("/"); 並將webAppRootKey對應的webapp.root分別作為Key,Value寫到System Properties系統屬性中。之後在程序中通過System.getProperty("webapp.root")來獲得WebRoot的物理路徑。
具體示例代碼如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>csc2.root</param-value>
</context-param>
<listener>
<listener-class>test.ApplicationListener</listener-class>
</listener>
</web-app>

ApplicationListener.java
package test;
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoaderListener;
public class ApplicationListener extends ContextLoaderListener {
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
String webAppRootKey = sce.getServletContext().getRealPath("/");
System.setProperty("csc2.root" , webAppRootKey);
String path =System.getProperty("csc2.root");
System.out.println("sssss:::"+path);
}
}

test.java
public class test {
public void remve(){
String path =System.getProperty("csc2.root");
System.out.println("result::::::::"+path);
}

}

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="test.test" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
test t = new test();
t.remve();
%>
<html>
</html>
部署程序發布 啟動TOMCAT 運行index.jsp 就可以調用JAVA中全局設置的物理路徑了(說明這里的JSP 只是調用了TEST.JAVA 的remove方法,不做其他使用。原理解釋,TOMCAT啟動和讀取WEB.XML 監聽方式載入SPRING ApplicationListener繼承SPRING ContextLoaderListener載入SPRING順便吧全局路徑賦值給csc2.root 描述,這樣之後JAVA 代碼中就可以使用System.getProperty("csc2.root")調用全路路徑了。

2. java web怎麼訪問伺服器指定路徑

可以把文件目錄配置在web.xml文件的初始化參數中, 通過ServletAPI讀取文件目錄

比如
定義一個Properties文件保存相關配置
#可以上傳文件的後綴名
extensions=pptx,docx.doc,txt,jpg,jar
#單個文件的大小1M
fileMaxSize=1048576
#總共上傳文件大小5M
totalFileMaxSize=5242880
#文件保存路徑
filePath=z:/temp
#臨時文件路徑
tempDir=z:/temp/temp

使用Listener在伺服器啟動時載入配置信息

1
2
3
4
5
6
7
8
9
10
11

ServletContext context = event.getServletContext();
InputStream inputStream = context
.getResourceAsStream("/WEB-INF/classes/file/upload/commons/uploadConfig.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
context.setAttribute("fileConfig", properties);
System.out.println("properties = " + properties.size());
} catch (IOException e) {
e.printStackTrace();
}

在你上傳文件時通過配置文件讀取路徑保存
String filePath = ((Properties) this.getServletContext().getAttribute("fileConfig"))
.getProperty(FileUploadConstants.FILE_PATH);

熱點內容
bs源碼 發布:2025-02-01 16:51:52 瀏覽:566
百度雲zip怎麼解壓 發布:2025-02-01 16:31:59 瀏覽:566
pmc密碼是什麼意思 發布:2025-02-01 16:28:49 瀏覽:783
蘋果手錶和安卓手錶哪個好用 發布:2025-02-01 16:28:07 瀏覽:582
上海電信上傳 發布:2025-02-01 16:22:10 瀏覽:982
有什麼好的雙u主板配置 發布:2025-02-01 16:18:17 瀏覽:177
編程漩渦 發布:2025-02-01 16:01:51 瀏覽:328
司機會所訪問 發布:2025-02-01 15:54:11 瀏覽:780
家用電腦改成伺服器並讓外網訪問 發布:2025-02-01 15:30:23 瀏覽:355
javac工資 發布:2025-02-01 15:24:28 瀏覽:23