Demand
The project was developed using Springboot and deployed as a jar package. File uploads in the project are saved to the upload directory under D judgment.
Enter http://localhost:8080/upload/logo_1.jpg in the browser to access the logo_1.png image in the D-Disk upload directory
Workaround
Because of the way the jar package is used, you cannot use the way you configure a virtual directory for Tomcat to set up a virtual directory for springboot built-in Tomcat.
Realize
Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration Public classWebmvcconfigextendsWebmvcconfigureradapter {@Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {Registry.addresourcehandler ("/upload/**"). Addresourcelocations ("file:d:/upload/")); }}
Start the project, enter the http://localhost:8080/upload/logo_1.jpg in the browser, you can access the picture normally.
In the above example, the Interceptor interception request and file upload directory are all written dead, can be placed in the Application.properties configuration file, easy to modify. The modified code looks like this:
Application.properties
server.port=8080# static resources externally exposed access path file.staticaccesspath=/upload/** #文件上传目录file. uploadfolder=d:\\upload
Fileuploadproperteis.java
Importorg.springframework.boot.context.properties.ConfigurationProperties;Importorg.springframework.stereotype.Component;/*** File Upload related properties * @create 2018-04-22 13:58 **/@Component @configurationproperties (prefix= "File") Public classFileuploadproperteis {//access paths for static resources exposed externally PrivateString Staticaccesspath; //File upload directory PrivateString Uploadfolder; PublicString Getstaticaccesspath () {returnStaticaccesspath; } Public voidSetstaticaccesspath (String staticaccesspath) { This. Staticaccesspath =Staticaccesspath; } PublicString Getuploadfolder () {returnUploadfolder; } Public voidSetuploadfolder (String uploadfolder) { This. Uploadfolder =Uploadfolder; }}
Webmvcconfig.java
ImportCom.lnjecit.springboothelloworld.properties.FileUploadProperteis;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration Public classWebmvcconfigextendsWebmvcconfigureradapter {@AutowiredPrivateFileuploadproperteis Fileuploadproperteis; @Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {Registry.addresourcehandler (fileuploadproperteis.gets Taticaccesspath ()). Addresourcelocations ("File:" + fileuploadproperteis.getuploadfolder () + "/"); }}
Demo:https://github.com/linj6/springboot-learn/tree/master/springboot-helloworld
Resources
1, https://liuyanzhao.com/7599.html
springboot-setting up virtual directories for built-in Tomcat