0

I'm learning spring boot vs 2.1.1

I would like to display in the template image from an external folder.

There is the app structure in tomcat 9:

|-webApp/ws-app     // where ws-app is the running app
|
|-webData           // where I would like to get the image to display in template

I've read many article from 5 years ago and I've tried to used them without success. Like this one: link

I've tried to add in application.properties this code:

spring.resources.static-locations=file:///C:/TMP/webData/images

Access it from the template like this:

http://localhost:8100/ws-app/img.jpg

I get 404 error

I've tried this coding too by creating a class like this :

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class CustomWebMvcAutoConfig implements WebMvcConfigurer {

    String myExternalFilePath = "file:///C:/TMP/webData/images";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations(myExternalFilePath);
    }
}

The same error 404

Someone can help me to understand what I'm doing wrong or give me the correct way if what I'm trying to do is obsolete ?

Thanks so much

JBD
  • 568
  • 8
  • 25

1 Answers1

3

you should end your external-file-path with a /

you do not need to add @AutoConfigureAfter(DispatcherServletAutoConfiguration.class)

use http://localhost:8100/ws-app/images/img.jpg to access image if your image is stored as C:/TMP/webData/images/img.jpg

@Configuration
public class CustomWebMvcAutoConfig implements WebMvcConfigurer {

    String myExternalFilePath = "file:C:/TMP/webData/images/"; // end your path with a /

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations(myExternalFilePath);
    }
}
Dirk Deyne
  • 6,048
  • 1
  • 15
  • 32
  • Thanks so so much! With the correction of the myExternalFilePath, plus the port 8080 and not 8100 as I wrote (my bad) it works well – JBD Dec 11 '19 at 17:45