Spring boot image display url

Viewed 2457

Hello i have images folder like this

enter image description here

What i want to do, is upload images to that folder and display them like this localhost:8080/image.jpg , so i can display it in frontend.. Nothing wrong is with upload, but i have no idea with displaying this images with api.

I already have tried

@Configuration
public class AdditionalResourceWebConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:images/");
    }
}

But this is not working..

2 Answers

What i want to do, is upload images to that folder and display them like this localhost:8080/image.jpg

URL should be localhost:8080/images/image.jpg as you are saving the images in images folder and mapping them as /images in addResourceHandler method

@Configuration
public class AdditionalResourceWebConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                  .addResourceLocations("file:images\\");
    }
}

The correct syntax of using file: is file:///. I would rather keep the static resources inside resources/static directory from where it should automatically be picked up by spring boot. Here is the documentation related to resource handling in spring

Related