Reputation: 12024
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
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
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.
@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());
}
}
PathResourceResolver
in the resource chain as the sole ResourceResolver
in it.PathResourceResolver
, locates the /first.jpg
file in the /Users/tom/myimages
folderUpvotes: 5
Reputation: 823
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
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