Three ways for Spring Boot to obtain files in the resources directory

In the Spring Boot project, it is often necessary to obtain resourcesthe files in the directory. These files can include configuration files, template files, static resources, etc. This article will introduce three commonly used methods to obtain resourcesfiles in a directory.

1. Using ResourceLoaderthe interface

ResourceLoaderThe interface is the interface provided by the Spring framework for loading various resources, including classpaththe resources below. In Spring Boot, the files in the directory can be ResourceLoaderobtained through the dependency injection interface . resourcesHere is an example:

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

@Component
public class YourComponent {
    
    
    
    private final ResourceLoader resourceLoader;
    
    public YourComponent(ResourceLoader resourceLoader) {
    
    
        this.resourceLoader = resourceLoader;
    }
    
    public void getResource() throws IOException {
    
    
        Resource resource = resourceLoader.getResource("classpath:your-file.txt");
        InputStream inputStream = resource.getInputStream();
        // 对文件进行操作,比如读取内容等
    }
}

In the above code, we injected ResourceLoaderthe instance of the interface through the constructor. Then, use resourceLoader.getResource("classpath:your-file.txt")the method to get your-file.txtthe file's Resourceobject. Through Resourcethe object, we can get the input stream of the file and operate on it.

2. Using ClassPathResourceclasses

ClassPathResourceThe class is the class provided by the Spring framework for loading resources under the class path. In Spring Boot, we can use ClassPathResourceclasses to get resourcesfiles in a directory. Here is an example:

import org.springframework.core.io.ClassPathResource;

public void getResource() throws IOException {
    
    
    ClassPathResource resource = new ClassPathResource("your-file.txt");
    InputStream inputStream = resource.getInputStream();
    // 对文件进行操作,比如读取内容等
}

In the above code, we use ClassPathResourcethe class to get your-file.txtthe file. It looks for files directly from the classpath and returns an Resourceobject.

3. ResourceUtils.getFile()How to use

ResourceUtilsClasses are utility classes provided by the Spring framework for manipulating resources. In Spring Boot, we can use ResourceUtils.getFile()methods to get resourcesfiles in a directory. Here is an example:

import org.springframework.util.ResourceUtils;

public void getResource() throws IOException {
    
    
    File file = ResourceUtils.getFile("classpath:your-file.txt");
    // 对文件进行操作,比如读取内容等
}

In the above code, we use ResourceUtils.getFile()the method to get your-file.txtthe file. It returns an Fileobject that can be directly manipulated on the file.

4. Precautions

When using the above methods to obtain resourcesfiles in a directory, please note the following:

  • Make sure the file path and name are correct and that the file is resourcesin the directory.
  • If using `ResourceLoader

Guess you like

Origin blog.csdn.net/qq_39997939/article/details/130976975