Spring boot uses external resource files to upload files

The static resource path refers to the path that the system can directly access, and all files under the path can be directly read by the user.

The default static resource paths in Springboot are: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/, from which it can be seen that the static resource paths here are all in classpath(that is, the folders specified under the project path)

Imagine such a situation: a website has the function of uploading files, what will be the consequences if the uploaded files are placed in the above folders?

  • Website data and program code cannot be effectively separated;
  • When the project is packaged into a file for deployment, how inefficient it is .jarto put the uploaded file into this file;.jar
  • Backups of website data will be a pain.

Probably the best solution at this point is to set the static resource path to the base directory on disk.

In Springboot , you can directly override the configuration information of the default static resource path in the configuration file:



 web:

  upload-path: D:/tts/

Note:web.upload-path This is a custom attribute that specifies a path, which should /end with;

spring.mvc.static-path-pattern=/**Indicates that all accesses go through the static resource path;

spring.resources.static-locationsConfigure the static resource path here. As mentioned earlier, the configuration here is to override the default configuration, so you need to add the default one. Otherwise static, publicthese paths will not be regarded as static resource paths. file:${web.upload-path}All you need to add at the end file:is Because a specific hard disk path is specified, other uses classpathrefer to system environment variables

 
Reference source:
 http://blog.csdn.net/kilua_way/article/details/54601195
https://github.com/zsl131/spring-boot-test

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326391567&siteId=291194637