server.port=8081 server.servlet.context-path=/spring-boot-demo spring.application.name=spring-boot-demo
server.port=8081 server.servlet.context-path=/spring-boot-demo spring.application.name=spring-boot-demo spring.resources.static-locations=classpath:static/,classpath:files/
通过spring.resources.static-locations指定静态资源的路径,将新建的files/路径添加进配置里,因为想要继续能够访问static下的文件,所以将默认的static也添加进去,否则会报错404,,访问c.jpg效果图如下
server.port=8081 server.servlet.context-path=/spring-boot-demo spring.application.name=spring-boot-demo
代码中实现WebMvcConfigurer,覆盖addResourceHandlers(ResourceHandlerRegistry registry)方法,如下
package com.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:static/")
.addResourceLocations("classpath:files/")
.addResourceLocations( "file:F:\\opt\\data\\files\\");
}
}
原文:https://www.cnblogs.com/dsxie/p/12992006.html