默认Springboot将从如下位置按如下顺序加载jar包对应前端静态资源:1.jar包同级static目录 在调试模式下,Springboot将从class目录中按如下顺序加载对应前端静态资源1.class目录下static目录 通过设置spring.resources.static-locations自定义Spring boot加载前端静态资源路径spring.resources.static-locations: file:D:/public/ 如果指定了拦截器,该属性有可能失效,需要在拦截器处排除映射前缀 : : /** : : classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:d:/spas/data 关键java代码 public class AuthWebMvcConfigurer implements WebMvcConfigurer { @Value("${mvp51.file-upload.local.path}") private String path; @Override public void addInterceptors(InterceptorRegistry registry) { registry .addInterceptor(new AuthInterceptor()) .excludePathPatterns("/static/**") .excludePathPatterns("/upload/**") .order(-100); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**") .addResourceLocations("classpath:/static/") . dResourceLocations("file:"+path+"/"); } http://localhost:9080/api/upload/test2/apple.jpg |
|
来自: 明神月 > 《springboot》