SoFunction
Updated on 2025-04-14

springboot gets the location of the static directory under resources

In Spring Boot, if you want to get itresourcesIn the directorystaticThe directory location can be accessedResourceLoaderOr use it directlyPathclass to get file path.

Spring Boot will automaticallysrc/main/resources/staticThe static resources in the directory are exposed, so you can obtain them in the following waysstaticResources in the directory.

Method 1: Use ResourceLoader to get the static directory path

Spring Boot will automatically launchstaticThe directory map is/staticpath so you can passResourceLoaderto 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/staticIn 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 usePathClass to getstaticFile path in the directory. You can use Spring BootApplicationContextto 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 useServletContextCome and get itstaticThe 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 defaultstaticThe resources in the directory are exposed to the web root directory, and you can access them directly through the browser./staticpath.

("/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 returnnullIn some containers (for example, in embedded Tomcat).

Summarize

If you want to access the Spring BootstaticThe most common method for files in a directory is toResourceLoaderorServletContextto 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!