首页 > 解决方案 > SpringBoot 从自定义路径提供静态图像

问题描述

现在我一直通过application.properties文件中的静态路径提供我的图像:

spring.resources.staticlocations=file:/Applications/MAMP/htdocs/reportMaker/template
spring.mvc.static-path-pattern=/resources/**

然后通过做

http://localhost:8080/resources/logo.png

我能够到达徽标。

现在我的目标是使用从我的数据库中获取的文件夹路径进行切换。

我试过这种方法:

@EnableWebMvc
@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {

@Autowired
ConfigurationRepository confRepo;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    myConfiguration conf = confRepo.findByConfKey("downloadPath");
    String path =  conf.getConfValue();

    if(path !=null) {
        registry.addResourceHandler("/resources/**").addResourceLocations(path);
    }
}

但是我无法以与以前相同的方式到达徽标。

路径变量是/Applications/MAMP/htdocs/reportMaker/template

标签: javaspring-bootstaticresource

解决方案


路径变量是/Applications/MAMP/htdocs/reportMaker/template

根据此文档https://www.baeldung.com/spring-mvc-static-resources路径应以file:/


推荐阅读