SpringBoot (springmvc)自定义静态文件的位置

SpringBoot默认把类路径下的/static,/public,/resources和META-INF/resources文件下的静态文件映射为了"/",可以直接访问。

自定义访问路径

@Configuration
public class WebMvcAutoConfiguration extends WebMvcConfigurationSupport {

    // 配置文件自定义访问路径 也可以通过启动参数修改
    @Value("${mole.work}")
    private String work;

    private static final String FILE_PROTOCOL = "file:///";

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 设置外部访问路径 
        registry.addResourceHandler("/upload/**")
                .addResourceLocations(FILE_PROTOCOL + work + "/upload/");
        // 设置内部访问路径
        registry.addResourceHandler("/assets/**")
                .addResourceLocations("classpath:/admin/assets/")
        super.addResourceHandlers(registry);
    }
}
# java   spring   springboot  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×