In Spring Boot, if you want to get itresources
In the directorystatic
The directory location can be accessedResourceLoader
Or use it directlyPath
class to get file path.
Spring Boot will automaticallysrc/main/resources/static
The static resources in the directory are exposed, so you can obtain them in the following waysstatic
Resources in the directory.
Method 1: Use ResourceLoader to get the static directory path
Spring Boot will automatically launchstatic
The directory map is/static
path so you can passResourceLoader
to load it.
import ; import ; import ; import ; import ; @Service public class StaticResourceService { @Autowired private ResourceLoader resourceLoader; public void getStaticResource() throws IOException { // Get the resources in the static directory Resource resource = ("classpath:/static/"); if (()) { ("Resource exists at: " + ()); } else { ("Resource not found!"); } } }
In this example,("classpath:/static/")
Will loadsrc/main/resources/static
In the directorydocument. If the file exists, it prints out the URI of the file.
Method 2: Use Path to get the static directory path
If you need to get the absolute path to the static resource (for example, if you want to read the file content), you can usePath
Class to getstatic
File path in the directory. You can use Spring BootApplicationContext
to get the file path.
import ; import ; import ; import ; @Service public class StaticResourceService { @Value("${-locations}") private String staticLocations; public void getStaticPath() { // Obtain the absolute path to static resources Path path = (staticLocations + "/"); ("Static file path: " + ()); } }
Method 3: Get the static resource path through ServletContext
If you need to get the root path of the static resource, you can useServletContext
Come and get itstatic
The path to the folder:
import ; import ; import ; @Service public class StaticResourceService { @Autowired private ServletContext servletContext; public void getStaticPath() { // Get the physical path to the static directory String staticPath = ("/static"); ("Static directory path: " + staticPath); } }
Note
classpath:/static
: Spring Boot will defaultstatic
The resources in the directory are exposed to the web root directory, and you can access them directly through the browser./static
path.
("/static")
: If you need an absolute file path (i.e. the path on disk), which usually depends on the runtime environment and container configuration, it may returnnull
In some containers (for example, in embedded Tomcat).
Summarize
If you want to access the Spring Bootstatic
The most common method for files in a directory is toResourceLoader
orServletContext
to get the path or content of the file.
These methods are suitable for dynamically loading or manipulating static resources in Spring Boot applications.
This is the article about obtaining the location of the static directory under springboot resources. For more related content of obtaining the static location under springboot resources, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!