1.定义配置类WebConfig
package com.example.demo03.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 访问地址:如http://localhost:8081/api/file/1.jpg
registry.addResourceHandler("/api/file/**")
// 访问http://localhost:8081/api/file/1.jpg 会自动找E:\demo03\src\main\resources\static\images\ 路径下名为1.jpg图片
.addResourceLocations("file:" + "E:\\demo03\\src\\main\\resources\\static\\images\\");
}
}
在浏览器中访问http://localhost:8081/api/file/1.jpg就可以看到图片