springboot+vue实现本地图片访问

后端代码,实现WebMvcConfigurer类,重写addResourceHandlers方法,定义虚拟路径,同时有拦截时对/img/**放行

package com.seu.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.beans.factory.annotation.Value;
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
    //文件地址
    @Value("${user.avatar.path}")
    private String USER_AVATAR_PATH;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("file:"+USER_AVATAR_PATH );
    }
}

前端代码,前端直接通过(/img/+图片名)即可访问

<img class="header-picture" :src="'/img/'+updateDataFrom.avatar">
    

 

posted @ 2020-12-09 18:54  小小穿梭机^^  阅读(1726)  评论(0编辑  收藏  举报