Spring Boot images uploading and serving

前端 未结 3 1526
南方客
南方客 2020-12-30 10:22

I\'m making new Spring Boot app and want to be able to store and serve images, I want images to be stored in applications directory:

this is what uploading look

3条回答
  •  我在风中等你
    2020-12-30 10:56

    To access image from your images folder,

    You need to override addResourceHandlers method of WebMvcConfigurerAdapter class like this:

    @Configuration
    public class ResourceConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/images/**").addResourceLocations("file:images/");
        }
    }
    

    After that you need add / before images in URL like this:

    
    

提交回复
热议问题