I am using Spring Boot. I try using images from local resources to show it on webpage.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Value("{upload.path}")
private String uploadPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**")
.addResourceLocations("file://" + uploadPath + "/");
}
}
File application.properties
upload.path=/e:/fortests
when I am trying to localhost:8080/img/image.jpeg
I received this log
2018-12-31 02:46:32.242 DEBUG 11972 --- [nio-8080-exec-8]
o.s.web.servlet.DispatcherServlet : GET "/img/image.jpeg", parameters={}
2018-12-31 02:46:32.245 DEBUG 11972 --- [nio-8080-exec-8]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to
ResourceHttpRequestHandler ["file://{upload.path}/"]
2018-12-31 02:46:32.250 DEBUG 11972 --- [nio-8080-exec-8]
o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2018-12-31 02:46:32.251 DEBUG 11972 --- [nio-8080-exec-8]
o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2018-12-31 02:46:32.253 DEBUG 11972 --- [nio-8080-exec-8]
o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2018-12-31 02:46:32.259 DEBUG 11972 --- [nio-8080-exec-8]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public
org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-12-31 02:46:32.267 DEBUG 11972 --- [nio-8080-exec-8]
o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given
[text/html, text/html;q=0.8]
2018-12-31 02:46:32.269 DEBUG 11972 --- [nio-8080-exec-8]
o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
if not using path.upload
property and make it hard-coded in class
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**")
.addResourceLocations("file://e:/fortest/");
}
log:
2018-12-31 02:51:03.120 DEBUG 13408 --- [nio-8080-exec-1]
o.s.web.servlet.DispatcherServlet : GET "/img/image.jpeg", parameters={}
2018-12-31 02:51:03.121 DEBUG 13408 --- [nio-8080-exec-1]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to
ResourceHttpRequestHandler ["file://e:/fortest/"]
2018-12-31 02:51:07.687 DEBUG 13408 --- [nio-8080-exec-1]
o.s.web.servlet.DispatcherServlet : Failed to complete request:
java.net.UnknownHostException: e
2018-12-31 02:51:07.689 ERROR 13408 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].
[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet]
in context with path [] threw exception
at first I received whitelabel page, now I just received "non-loaded" image. What am I doing wrong, and what better way to handle local resources if not this for me? and yes, I have image.jpeg
in /e:/fortest
(1) You are using Windows operating system. This snippet
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**")
.addResourceLocations("file://e:/fortest/");
}
Line
.addResourceLocations("file://e:/fortest/");
should be
.addResourceLocations("file:///E:/fortest/");
(2)
@Configuration
public class MvcConfig implements WebMvcConfigurer {
should be
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {