SpringBoot opens a path as a static resource directory

During development, we may need to open some static resource files to front-end access, but we don’t want to stuff these files into the project (mainly because the files take up too much memory), so what should we do?

In fact, springboot provides a capability that allows you to open any folder in the local directory to front-end access.

1. First, we create a new folder on the local disk, like this

insert image description here
Need to pay attention: which disk your project is on, you will create this folder on that disk

2. Then we put a file in this folder

insert image description here

3. Then we add configuration in the configuration file of springboot

spring: 
            
    # 静态文件路径映射 
    resources: 
        static-locations: file:/upload-file

In this way, we /upload-filecan access the folder as a network resource

4. Start the project

insert image description here

5. Then we can access this 1.jpg file from the browser

insert image description here

OK, the test was successful


write to idiot know

Guess you like

Origin blog.csdn.net/shengzhang_/article/details/123580683