ace
ace

Reputation: 12024

How to specify external location for image store in Spring Boot 2.0 web app?

Currently I have images stored in /src/main/resources/static/myimages in the project directory. Now I want to move these outside of project directory like in /Users/tom/myimages so that in img tag src="/myimages/subdir/first.jpg" in the HTML markup will be loaded from /Users/tom/myimages/subdir/first.jpg. How can I achieve this in spring boot 2.0 project?

This will allow me to add new images without having to recompile the project in production environment.

Upvotes: 9

Views: 7304

Answers (4)

Robert Ellis
Robert Ellis

Reputation: 734

The above answers are very good .but the correct way to do this would according to spring boot docs .you need to add the following property to the application.properties file

spring.resources.static-locations=file:/home/username/images/

The mvc static path pattern is not necessary spring.mvc.static-path-pattern=/resources/**

http://localhost:8080/resources/exampleimage.png

The resource path we mentioned would be added to the list of the default resources locations

please read the docs for more info https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-static-content

Upvotes: 0

Mohammadreza  Alagheband
Mohammadreza Alagheband

Reputation: 2230

You could achieve this, by PathResourceResolver which is the simplest resolver and its purpose is to find a resource given a public URL pattern. In fact, this is the default resolve.

Code:

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry
               .addResourceHandler("/myimages/**")
               .addResourceLocations("/Users/tom/myimages")
               .setCachePeriod(3600)
               .resourceChain(true)
               .addResolver(new PathResourceResolver());
    }
} 

Description:

  • We are registering the PathResourceResolver in the resource chain as the sole ResourceResolver in it.
  • the html code that, in conjunction with the PathResourceResolver, locates the /first.jpg file in the /Users/tom/myimages folder

Upvotes: 5

Vipul Gulhane
Vipul Gulhane

Reputation: 823

Spring 5 - Static Resources

From the documentation:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

        public void addResourceHandlers (ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/pages/**").
                      addResourceLocations("classpath:/my-custom-location/","C:/spark/Hadoop/my-custom-location/");


        }
}

or You can store Image path in Database. Load it in dynamically when ever required also you can change that path on fly.

HTML Code Will be like this :-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<link href='<spring:url value="/resources/css/style.css"/>' rel="stylesheet" />
<script type="text/javascript" src='<spring:url value="/resources/js/app.js"/>'></script>

</head>
<body>
   <h1 id="title" class="color1">Spring MVC- Static Resource Mapping Example</h1>

   <button onclick="changeColor()">Change Color</button>
   <hr />

   <img alt="http://mytechnologythought.blogspot.com" src="<spring:url value="/pages/img01.png"/>" width="200">
</body>
</html>

Here Full Example Reference Blog link Note :- The type WebMvcConfigurerAdapter is deprecated

Upvotes: 2

severus256
severus256

Reputation: 1723

May be you should try to define your own static resource handler ?

It will override the default one.

Something like this:

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/folder/");
    }
}

UPDATE That one is something seems to be work on some of my old projects:

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class CustomWebMvcAutoConfig extends
                    WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String myExternalFilePath = "file:///C:/Users/tom/imgs/";

    registry.addResourceHandler("/imgs/**").addResourceLocations(myExternalFilePath);

    super.addResourceHandlers(registry);
  }

}

Upvotes: 5

Related Questions