Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the static resource access configuration and the built-in tomcat virtual file mapping path in SpringBoot

Shulou Source: shulou.com Published: 2022-06-03 08:53:47 10月27日 Update

This article shows you SpringBoot static resource access configuration and built-in tomcat virtual file mapping path is how, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article hope you can get something.

Static resource access configuration @ Configuration@EnableWebMvcpublic class StaticResourceConfig implements WebMvcConfigurer {@ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ static/**") .addResourceLocations ("classpath:/static/");}} built-in tomcat virtual file mapping path @ Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter {@ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ image/**") .addResourceLocations ("file:C:/image/") }} Application scenario

Separate the file and put it in another drive letter, for example, my service is in / server file / data

Normally, the files placed under the webapps of the corresponding project can be accessed properly. Now I need to access the contents of the E disk, so I need to customize the static resource mapping.

Realization method

The implementation class inherits WebMvcConfigurerAdapter and overrides the method addResourceHandlers, as follows

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.WebMvcConfigurerAdapter;@Configurationpublic class FileServerConfig extends WebMvcConfigurerAdapter {@ Value ("${local.fileserver.dir}") private String localFileServerDir; @ Value ("${local.fileserver.path}") private String localFileServerPath / / access the picture method @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/" + this.getLocalFileServerPath () + "/ * *") .addResourceLocations ("file:" + this.getLocalFileServerDir () + "/") / / the local folder should start with "flie:" and the folder should end with "/". Example: / / registry.addResourceHandler ("/ abc/**") .addResourceLocations ("file:D:/pdf/"); super.addResourceHandlers (registry) } / * the actual path of the file changes to the server url path * @ param absolutePath * @ return * / public String toServerPath (String absolutePath) {absolutePath = absolutePath.replaceAll ("\", "/"); return "/" + absolutePath.replace (localFileServerDir, localFileServerPath);} public String getLocalFileServerDir () {return localFileServerDir } public void setLocalFileServerDir (String localFileServerDir) {this.localFileServerDir = localFileServerDir;} public String getLocalFileServerPath () {return localFileServerPath;} public void setLocalFileServerPath (String localFileServerPath) {this.localFileServerPath = localFileServerPath;}}

Note: the local folder should start with "flie:", and the folder should end with "/". Example:

Registry.addResourceHandler ("/ pdf/**") .addResourceLocations ("file:D:/pdf/")

It means that your server path serverdata corresponds to your local / data directory. If you want to access / data/test.jpg images, the corresponding server path is http://localhost:serverdata/test.jpg.

The above is what the static resource access configuration and the built-in tomcat virtual file mapping path are like in SpringBoot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: File path resource static folder service configuration content method server picture beginning skill knowledge concise concise code scenario actual is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Shulou Information Huawei Linux