File systems are a common problem in our development process. So what does file access look like in the Spring Boot framework? Let me conclude with you today.
File and classpath
A file that is stored in the actual location on your computer. In Spring Boot, it begins with file:. Such as:
file:a.txt
The a.tb file in the current directory. In a development environment, the current path is generally in the Maven project directory (the same directory as pom.xml). After being packaged as a JAR file, the current path is the path where the JAR file is run.file:D:\a.txt
Represents the absolute path, which will not be explained here.
Inside a JAR File, we usually refer to the File path as the classpath, so reading an internal File is read from the classpath. The File specified by the classpath cannot be parsed into a File object, but can be parsed into an InputStream. Such as:
classpath:/a.txt
Jar package root directory a.tb. The classpath starting with/indicates the absolute path, which is the root directory of the JAR package.
2. Static resource access for Spring Boot
We all know that the SRC main resources in the Spring project folder is the place to store resources. By default, the static resource location after Spring packaging is as follows:
- classpath:/static
- classpath:/public
- classpath:/resources
- classpath:/META-INF/resources
In Spring, the root of the classpath is in the project foldersrc\main\resources
.
Here’s an example:
In the project folder SRC \main\resources\static place the image qiqi.png:
127.0.0.1:8080/qiqi.png
This example shows a one-to-one mapping between the externally accessed resource path and the resource file path in the Spring project. The “root directory” for external access corresponds to the four static resource locations (CLASspath) described above.
You can also create a new Controller class by writing the following method:
@GetMapping("/pic")
public String ShowPic() {
return "/qiqi.png";
}
Copy the code
Run, access 127.0.0.1:8080/ PIC, same effect as above.
In the Controller method, GetMapping is the route path and return is the corresponding resource path.
The default resource path can be changed.
We need to know that the following two configurations can be added to the configuration file application.properties:
spring.mvc.static-path-pattern
spring.resources.static-locations
Copy the code
Spring.mvc. static-path-pattern indicates the path that should be used to access static resources. In other words, Spring Boot processes static resource requests only when the matching conditions of static resources are met. From the example above we know that this configuration defaults to /**. Suppose you add the following in the above project configuration file:
spring.mvc.static-path-pattern=/resources/**
Copy the code
Visit again so we’re going to visit the photo url: 127.0.0.1:8080 / resources/qiqi. PNG
In addition, spring.resources.static-locations specifies the path to static resource files, which are found in sequence depending on the configuration. As you can see from the example above, the default value is:
classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
Copy the code
In fact, the Spring default resource file location mentioned above is essentially the value of this configuration.
Suppose the configuration file in the above project reads:
spring.resources.static-locations=classpath:/myRes
Copy the code
To access qiqi.png, place it in the SRC \main\resources\myRes folder of the project folder:
After this item is configured, the default value will become invalid!
You can also use a disk path such as:
spring.resources.static-locations=file:res
Copy the code
That is, specify that the resource file is in the res folder in the project folder (that is, in the RES folder under the run path when the JAR file is packaged). You can also use absolute paths.
In general terms, the spring.mVC. Static-path-pattern configuration specifies the “root directory” for our external access, and the spring.resources. Static-locations configuration configures the corresponding resource location.
3, Spring Boot configuration file external
We also know that Spring is the default Boot configuration file located in the classpath: / application. The properties, the default will be packaged into a jar file.
We can also change the location of this configuration file.
Add the following annotation to our Spring main class:
@propertysource (value={" custom configuration file path "})Copy the code
Value indicates the location of the configuration file.
@propertysource (value={" PropertySource ", "PropertySource "})Copy the code
Here’s the full code for my main class:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource(value={"file:self.properties"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Copy the code
The configuration file is defined as the self.properties file in the project folder (the self.properties file in the run directory when the JAR file is packaged).