1

I have a folder named uploads which is at the same level as src folder. I upload images to this folder. Then I added the following configuration to be able to serve the images in thymeleaf:

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

I try to serve the images in Thymeleaf like this:

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

where ${photo} is the name file name.

However I get the following error:

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

Apparently the path in my configuration is wrong. Could somebody please tell me what I'm doing wrong?

MehdiB
  • 870
  • 12
  • 34

1 Answers1

3

There is missing file. Add below configuration:

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

here you get details.

GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39