第一种方式

注入一个WebMvcConfigurer对象(springboot中所有的WebMvcConfigurer对象会一起起作用)

package cn.hidove.forum.configurer;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfigurer {
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                System.out.println(System.getProperty("user.dir"));
                registry.addResourceHandler("/images/**").addResourceLocations("file:" + System.getProperty("user.dir") + "/images/");
            }
        };
    }
}

第二种方式:配置文件中配置(常用)

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:static/,file:D:/JavaWorkSpace/forum/images/