springboot不需要重启访问最新的文件

本文介绍了如何在SpringBoot应用中实现无需重启就能访问到最新文件的方法,包括确保接口不受拦截器限制,以及启动时调用接口的设置。通过发送请求/files/**,可以直接访问到文件目录下的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tip 

1.如果有拦截器,请开放该接口

2.项目启动时会调用该接口

3.请求的连接 http://localhost:8848/files/test.png

应用场景:

发起请求/files/**,访问文件的目录下的路径。

package com.fusdom.config;

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

/**
 * @创建人: giao
 * @创建时间: 2020/8/4
 * @描述: 解决跨域问题
 */
//@Configuration注解
@Configuration
//实现WebMvcConfigurer
public class CrossDomainConfig implements WebMvcConfigurer {
    //重写addCorsMappings方法
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "OPTIONS")
                .maxAge(3600);

    }


//    /**
//     * 这里需要先将限流拦截器入住,不然无法获取到拦截器中的redistemplate
//     * @return
//     */
//    @Bean
//    public WeChatLoginInterceptor getWeChatLoginInterceptor() {
//        return new WeChatLoginInterceptor();
//    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //获取文件的真实路径
//        String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\asserts\\img\\wx_icon\\";
//        System.out.println(path);
        //static/upload/**是对应resource下工程目录
        registry.addResourceHandler("/files/**").addResourceLocations("file:D:\\Git\\ht\\micro_services\\cloud_base\\src\\main\\resources\\static\\asserts\\img\\gogo\\");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值