How to configure resource folder in Spring-boot project
Preface
Under what circumstances need to configure resource folders?When the project needs to read files other than the static folder, it needs to be configured as a resource folder so that it can be accessed by users.
Application scenario: background management upload resources, when the foreground needs to view, for example: upload a video in the background, the user in the foreground needs to watch the video. Two projects need to be on the same server. At this time, the video file can be placed anywhere on the server in the background, and the front desk configures this location as a resource folder for users to access, then the user can read the video.
# 一、Configuration## 1. Configuration class
/**
* @ClassName SourcesConfig
* @Description 资源文件夹配置
* @Autor T_Antry
* @Date 2020/10/14 9:39
* @Version 1.0
*/
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SourcesConfig implements WebMvcConfigurer {
@Value("${download.path}")
private String downloadPath;//访问时的虚拟路径
@Value("${local.path}")
private String localPath;//服务器的绝对路径
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(downloadPath+"**").addResourceLocations("file:"+localPath);
}
}
2.properties file
local.path=/data3/xdja/
download.path=/videos/
2. Visit
1. Put in resources
Put your resources, videos, etc. under the absolute path of the server /data3/xdja/. Here is a video of the resource file you put in the demonstration, named "1.mp4"
2. Visit
Way 1
Enter the URL in the browser, for example:
http://182.92.126.192:8888/videos/1.mp4 182.92.126.192 is the server address
8888 is the port
/videos/ is the virtual path
1.mp4 is the file name The
renderings are as follows
Way 2
It can be placed under the video tag, and it can also be accessed by changing its src path to the above path