springboot配置虚拟路径以访问本地图片

springboot配置虚拟路径 以访问本地图片

我成功实现的方式是这样

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class UploadConfiguration extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/cc/**")//这个是虚拟路径图片路径
.addResourceLocations("file:E:/kuang/");//这个是图片真实路径
super.addResourceHandlers(registry);
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");//项目内的图片去static下找
}
}

html页面

<body>
<!--有thymeleaf和没有的写法-->
<img th:src="@{/cc/test.jpg}">
<img th:src="@{/cc/1.jpg}">
<img src="/cc/test.jpg">
<img src="/cc/1.jpg">
</body>

在此之前页试过实现 WebMvcConfigurer来重写addResourceHandlers方法 可是并不好用 比如这样

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//全面扩展springmvc
@Configuration
public class MVCConfig implements WebMvcConfigurer {
 @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/cc/**").addResourceLocations("file:E:/kuang/");
//        WebMvcConfigurer.super.addResourceHandlers(registry);
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}

网上都是这样实现的 我却不好用 希望大神指点

你可能感兴趣的:(springboot配置虚拟路径以访问本地图片)