https://blog.csdn.net/qq_38762237/article/details/81282444
前言
由于上传的图片是要前端展示出来的, 而磁盘里的图片有不能直接用本地路径直接访问, 这时就要把本地的图片路径映射成网页能访问的路径 localhost 了
解决办法
注意: 前面要加 file, 不然是访问不了的
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.Web.servlet.config.annotation.WebMvcConfigurerAdapter;
- /**
- * 图片本绝对路径与虚拟地址的映射
- *
- * @author yunxun
- *
- */
- @Configuration
- public class WebMvcConfig extends WebMvcConfigurerAdapter {
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- // 注意前面要加 file, 不然是访问不了的
- registry.addResourceHandler("/image/**").addResourceLocations("file:"+"F:");
- }
- }
你这就是相当于 F 盘映射成了 http://localhost:8080/image/
访问地址 http://localhost:8080/image / 你的文件路径
来源: http://www.bubuko.com/infodetail-3038793.html