In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.