Springboot file mapping configuration (file path)

One, based on yml or properties configuration file

The uploaded file is stored in a certain directory on a certain disk, such as E:/Practices/test

Specific placement:

web:

  upload-path: E:/Practices/test

  front-path: E:/Practices

spring:

  resources:

    static-locations: file:${web.upload-path},file:${web.front-path}

Access method: IP+port/file name

现在:http://localhost:8003/1442823076292.jpeg

Note: There is no need to configure what loading configuration class, the uploaded file is to E:/Practices/test. The file is shown as accessed above.

2. Based on the springboot configuration class, change the original default configuration

I will not repeat the upload process here, but briefly describe its uploading ideas:

The uploaded file is stored in a certain directory on a certain disk, such as E:/Video Production/Picture/

Map the local storage file directory to the project path through the configuration file class WebMvcConfig, check the configuration class for details

The project starts and the configuration class is loaded.

The configuration classes are as follows:

@Configuration

public class WebMvcConfig extends WebMvcConfigurationSupport {

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

String path = "E:\\Practices\\";//"/tools/images/";//"E:/视频制作/图片/";

// 上传路径映射 会使spring boot的自动配置失效

registry.addResourceHandler("/image/**").addResourceLocations("file:" + path);

super.addResourceHandlers(registry);

}

}

meaning:

Project access to the image path will directly access the files under the path path

interview method:

原来:file:///E:/%E8%A7%86%E9%A2%91%E5%88%B6%E4%BD%9C/%E5%9B%BE%E7%89%87/1442823076292.jpeg

right now:

http://localhost:9001/image/1442823076292.jpeg

http://localhost:9001/image/one/1442823076292.jpeg

3. Access based on absolute path

Simple idea:

Obtain a directory under resources, such as the static folder. The method of obtaining is as follows:

File staticFilePath = ResourceUtils.getFile("classpath:static");

According to the obtained path, create a new path and upload the file. For details, see the method uploadToProject of AbsolutePathController

Based on the default configuration, there is no need to add configuration classes.

If you add a configuration class, change the original path situation, you need to add:

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

super.addResourceHandlers(registry);

}

 

Comparative analysis: The uploaded file is placed in the resources directory of the project, and the file actually loaded is the file under the absolute path, such as image/1442823076292.jpeg. The main principle is still file mapping. When visiting, directly http://localhost:9001/images/1442823076292.jpeg

Note: Projects started in the form of jar packages are not applicable. The host has not found a good plan for the time being. There is a good plan. Please share it a lot, thank you. You can debug and play locally. In production, it is recommended to use an external path

Four, access based on Nginx configuration

Simple idea:

Upload the file to a certain path, such as C:/file

Use Nginx server, configure the access path and map it to the file upload path

The following is the configuration of Nginx

Configure based on alias

location /static/ {

   alias /var/www/static/;

}

alas will treat the specified path as the file path

Note: "/" must be added to the directory specified by alias, that is, /var/www/static/ cannot be changed to /var/www/static. When accessing http://IP:PORT/static/index.html, the actual access The implementation is /var/www/static/index.html, similar to the configuration class based on springboot

Based on the configuration root

location /static/ {

  root /var/www/;

}

Root will splice the specified path to the file path before accessing

Note: The /static/ specified in location must actually exist in the /var/www/ directory specified by root. Visit http://127.0.0.1:7001/static/t.txt through a browser, then the file to access the server is /var/www/static/t.txt

 

Project example:

https://github.com/krycai/gc-framework/tree/master/gc-upload

 

Explain a more detailed example: https://www.javatt.com/p/11507

 

Guess you like

Origin blog.csdn.net/baidu_28068985/article/details/103840426