springboot獲取resources下static目錄的位置
在 Spring Boot 中,如果你想獲取 resources 目錄下的 static 目錄的位置,可以通過(guò) ResourceLoader 或者直接使用 Path 類來(lái)獲取文件路徑。
Spring Boot 會(huì)自動(dòng)將 src/main/resources/static 目錄下的靜態(tài)資源暴露出來(lái),因此你可以通過(guò)以下幾種方式來(lái)獲取 static 目錄下的資源。
方法 1:使用 ResourceLoader 獲取 static 目錄路徑
Spring Boot 會(huì)在啟動(dòng)時(shí)自動(dòng)將 static 目錄映射為 /static 路徑,因此你可以通過(guò) ResourceLoader 來(lái)加載它。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class StaticResourceService {
@Autowired
private ResourceLoader resourceLoader;
public void getStaticResource() throws IOException {
// 獲取 static 目錄下的資源
Resource resource = resourceLoader.getResource("classpath:/static/somefile.txt");
if (resource.exists()) {
System.out.println("Resource exists at: " + resource.getURI());
} else {
System.out.println("Resource not found!");
}
}
}
在這個(gè)例子中,resourceLoader.getResource("classpath:/static/somefile.txt") 會(huì)加載 src/main/resources/static 目錄下的 somefile.txt 文件。如果文件存在,它會(huì)打印出文件的 URI。
方法 2:使用 Path 獲取 static 目錄路徑
如果你需要獲取靜態(tài)資源的絕對(duì)路徑(例如,如果你想讀取文件內(nèi)容),可以使用 Path 類來(lái)獲取 static 目錄下的文件路徑。你可以通過(guò) Spring Boot 的 ApplicationContext 來(lái)獲取文件路徑。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.nio.file.Path;
import java.nio.file.Paths;
@Service
public class StaticResourceService {
@Value("${spring.resources.static-locations}")
private String staticLocations;
public void getStaticPath() {
// 獲取靜態(tài)資源的絕對(duì)路徑
Path path = Paths.get(staticLocations + "/somefile.txt");
System.out.println("Static file path: " + path.toString());
}
}
方法 3:通過(guò) ServletContext 獲取靜態(tài)資源路徑
如果你需要獲取靜態(tài)資源的根路徑,可以使用 ServletContext 來(lái)獲取 static 文件夾的路徑:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletContext;
@Service
public class StaticResourceService {
@Autowired
private ServletContext servletContext;
public void getStaticPath() {
// 獲取 static 目錄的物理路徑
String staticPath = servletContext.getRealPath("/static");
System.out.println("Static directory path: " + staticPath);
}
}
注意
classpath:/static:Spring Boot 默認(rèn)將 static 目錄下的資源暴露在 web 根目錄下,你可以直接通過(guò)瀏覽器訪問(wèn) /static 路徑。
ServletContext.getRealPath("/static"):如果你需要的是絕對(duì)文件路徑(即磁盤上的路徑),這通常依賴于運(yùn)行環(huán)境和容器配置,可能會(huì)返回 null 在某些容器中(例如,在內(nèi)嵌 Tomcat 中)。
總結(jié)
如果你想訪問(wèn) Spring Boot 中的 static 目錄中的文件,最常用的方法是通過(guò) ResourceLoader 或 ServletContext 來(lái)獲取文件的路徑或內(nèi)容。
這些方法適用于在 Spring Boot 應(yīng)用中動(dòng)態(tài)加載或操作靜態(tài)資源。
到此這篇關(guān)于springboot獲取resources下static目錄的位置的文章就介紹到這了,更多相關(guān)springboot獲取resources下static位置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot無(wú)法請(qǐng)求html等靜態(tài)資源文件webapp或者resources/static的問(wèn)題及解決方案
- springboot設(shè)置加載靜態(tài)資源的路徑(spring.resources.static-locations)
- SpringBoot獲取resources目錄下的文件
- SpringBoot下獲取resources目錄下文件的常用方法
- SpringBoot實(shí)現(xiàn)本地上傳文件到resources目錄
- SpringBoot如何讀取resources目錄下的文件
- springboot讀取resources下文件的方式詳解
相關(guān)文章
Java數(shù)組動(dòng)態(tài)擴(kuò)容的實(shí)現(xiàn)示例
本文主要介紹了Java數(shù)組動(dòng)態(tài)擴(kuò)容的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
關(guān)于IDEA創(chuàng)建spark maven項(xiàng)目并連接遠(yuǎn)程spark集群?jiǎn)栴}
這篇文章主要介紹了IDEA創(chuàng)建spark maven項(xiàng)目并連接遠(yuǎn)程spark集群,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Java線程協(xié)調(diào)運(yùn)行操作實(shí)例詳解
這篇文章主要介紹了Java線程協(xié)調(diào)運(yùn)行操作,結(jié)合具體實(shí)例形式詳細(xì)分析了Java線程協(xié)調(diào)運(yùn)行原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
詳解IntelliJ IDEA 中如何配置多個(gè)jdk版本即(1.7和1.8兩個(gè)jdk都可用)
這篇文章主要介紹了詳解IntelliJ IDEA 中如何配置多個(gè)jdk版本即(1.7和1.8兩個(gè)jdk都可用),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11
Springboot工程中使用filter過(guò)程解析
這篇文章主要介紹了springboot工程中使用filter過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
MyBatis-Plus updateById不更新null值的方法解決
用Mybatis-Plus的updateById()來(lái)更新數(shù)據(jù)時(shí),無(wú)法將字段設(shè)置為null值,更新后數(shù)據(jù)還是原來(lái)的值,本文就來(lái)詳細(xì)的介紹一下解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08

