spring-boot静态资源目录配置

spring-boot静态资源目录配置(配置js、css、图片等资源的位置)

spring-boot静态资源默认为/src/main/resources下的/static目录,可以通过application.properties的server.servlet.context-path属性配置
如:

server.servlet.context-path=/public

文件服务器配置

package com.zzsoft.statistics.config;

import java.nio.file.Paths;

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 FileServerConfig implements WebMvcConfigurer {
    // public static final String FileServerRootDir = "C:/Statistics/CurrentVersion/";

    public static String getRootDir() {
        String basePath = Paths.get("").toAbsolutePath().toString().replace(".", "");
        return basePath;
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // registry.addResourceHandler("/**").addResourceLocations("classpath:/static/").addResourceLocations("file:C:/Statistics/CurrentVersion/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/").addResourceLocations("file:"+ getRootDir());
    }

}
posted @ 2023-08-16 16:20  我的五年  阅读(50)  评论(0)    收藏  举报