zoukankan      html  css  js  c++  java
  • spring-boot配置外部静态资源的方法

    import java.io.File;
    
    import javax.servlet.Servlet;
    
    import lombok.extern.slf4j.Slf4j;
    
    import org.springframework.boot.autoconfigure.AutoConfigureAfter;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
    import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
    import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    import org.springframework.web.servlet.DispatcherServlet;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    @ConditionalOnWebApplication
    @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
    @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    @Order(Ordered.HIGHEST_PRECEDENCE )
    @AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
    @Slf4j
    public class StaticResourceConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            /*
            File directory = new File("");
            String path2=directory.getAbsolutePath();
            StringBuilder builder = new StringBuilder();
            builder.append("file:").append(path2).append("/m/");
    
            //registry.addResourceHandler("/**").addResourceLocations( builder.toString() );
            //String myExternalFilePath = "file:///C:/Temp/whatever/m/";
            log.info( builder.toString() );
            */
            
            registry.addResourceHandler("/m/**").addResourceLocations("file:m/");
            //registry.addResourceHandler("/m/**").addResourceLocations(builder.toString());
    
            super.addResourceHandlers(registry);
        }
    }
  • 相关阅读:
    SpringBoot项目启动Gzip压缩
    Docker学习(一)
    MySQL中text类型说明
    java MultipartFile多文件上传/通过文件夹解析所有文件
    java后端处理跨域
    SpringCloud Feign调用服务接口时,GET请求变成POST请求报错
    java8 LocalDateTime/LocalDate 获取各种日期类型
    IDEA 不能输入中文
    基于docker 搭建minio 文件服务器
    MySql数据库导入sql错误 Unknown collation: 'utf8mb4_0900_ai_ci'
  • 原文地址:https://www.cnblogs.com/feika/p/4543754.html
Copyright © 2011-2022 走看看