方式一(配置文件)
1.配置文件配置
#=========================== 图片代理 ============================
mvc:
static-path-pattern: /bc/images/**
resources:
static-locations: file:E:/
2.如何使用
访问 http://IP:PORT/bc/images/image_name.jpg
前端展示: <img src="/bc/images/image_name.jpg">
方式二(配置类)
1. java 配置类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Description 拿到你的图片
* @Author yanghanwei
* @Mail yanghanwei@geotmt.com
* @Date 2019/5/7 20:40
* @Version v1
**/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//在F:/SpringBootFiles/Image/下如果有一张 Excalibar.jpg的图片,那么:
//【1】访问:http://localhost:8080/imgs/Excalibar.jpg 可以访问到
//【2】html 中 <img src="/bc/images/Excalibar.jpg">
registry.addResourceHandler("/bc/images/**").addResourceLocations("file:E:/");
}
}
2.如何使用
访问 http://IP:PORT/bc/images/image_name.jpg
前端展示: <img src="/bc/images/image_name.jpg">