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.
3 Answers
Answers 1
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 soleResourceResolver
in it. - the html code that, in conjunction with the
PathResourceResolver
, locates the/first.jpg
file in the/Users/tom/myimages
folder
Answers 2
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); } }
Answers 3
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
0 comments:
Post a Comment