猿问

spring boot - 从 src 外的文件夹提供图像

我有一个命名的文件夹uploads是在同一级别的src文件夹中。我将图像上传到此文件夹。然后我添加了以下配置以便能够在 thymeleaf 中提供图像:


@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.

显然我配置中的路径是错误的。有人可以告诉我我做错了什么吗?


海绵宝宝撒
浏览 143回答 1
1回答

慕哥9229398

有缺失file。添加以下配置:@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void addResourceHandlers(ResourceHandlerRegistry registry) {&nbsp; &nbsp; &nbsp; &nbsp; registry&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addResourceHandler("/images/**")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addResourceLocations("file:resources/", "file:uploads/")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setCachePeriod(0);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答