Spring Boot-从src外部的文件夹提供图像

时间:2018-07-24 16:17:44

标签: java spring-boot thymeleaf

我有一个名为uploads的文件夹,它与src文件夹处于同一级别。我将图像上传到此文件夹。 然后,我添加了以下配置以能够在百里香叶中提供图像:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/uploads/**")
                .addResourceLocations("/resources/","/../../uploads/")
                .setCachePeriod(0);
    }
}

我尝试像这样在Thymeleaf中提供图像:

<img class="img-thumbnail img-responsive" src="#" th:src="@{'/uploads/' + ${photo}}" alt="">

其中${photo}是名称文件名。

但是我遇到以下错误:

The resource path [/../../uploads/rtf_vtvsq1r12q.png] has been normalized to [null] which is not valid.

很明显,我的配置路径错误。有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:2)

缺少 file 。添加以下配置:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/images/**")
                .addResourceLocations("file:resources/", "file:uploads/")
                .setCachePeriod(0);
    }
}

here您会获得详细信息。