springboot-setting up virtual directories for built-in Tomcat

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.