need
The project is developed using springboot and deployed as a jar package. All files uploaded 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 upload directory of the D drive
Solution
Because the jar package method is used, the method of configuring a virtual directory for tomcat cannot be used. It is necessary to set a virtual directory for the built-in tomcat of springboot.
accomplish
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/upload/"); } }
Start the project and enter http://localhost:8080/upload/logo_1.jpg in the browser to access the pictures normally.
In the above example, the request intercepted by the interceptor and the file upload directory are hard-coded, and can be placed in the application.properties configuration file for easy modification. The modified code is as follows:
application.properties
server.port=8080
#Access path for static resources exposed to the outside world
file.staticAccessPath=/upload/**
#File upload directory
file.uploadFolder=D:\\upload
FileUploadProperteis.java
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * File upload related properties * @create 2018-04-22 13:58 **/ @Component @ConfigurationProperties(prefix = "file") public class FileUploadProperteis { // The access path of static resources exposed to the outside world private String staticAccessPath; // File upload directory private String uploadFolder ; public String getStaticAccessPath() { return staticAccessPath; } public void setStaticAccessPath(String staticAccessPath) { this.staticAccessPath = staticAccessPath; } public String getUploadFolder() { return uploadFolder; } public void setUploadFolder(String uploadFolder) { this.uploadFolder = uploadFolder; } }
WebMvcConfig.java
import com.lnjecit.springboothelloworld.properties.FileUploadProperteis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Autowired private FileUploadProperteis fileUploadProperteis; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(fileUploadProperteis.getStaticAccessPath()).addResourceLocations("file:" + fileUploadProperteis.getUploadFolder() + "/"); } }
Demo download address: https://github.com/linj6/springboot-learn/tree/master/springboot-helloworld
References
1、https://liuyanzhao.com/7599.html