ResourceHttpRequestHandler:找不到资源/本地目录中的图像

ResourceHttpRequestHandler : resource not found / image from local directories

我正在使用 Spring Boot。我尝试使用本地资源中的图像在网页上显示它。

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 + "/");
    }

}

文件application.properties

upload.path=/e:/fortests

当我尝试 localhost:8080/img/image.jpeg

我收到了这个日志

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

如果不使用 path.upload 属性 并在 class

中进行硬编码
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/img/**")
            .addResourceLocations("file://e:/fortest/");
}

日志:

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

起初我收到的是whitelabel页面,现在我只收到"non-loaded"图片。我做错了什么,如果不是这对我来说,还有什么更好的方法来处理本地资源?是的,我在 /e:/fortest

中有 image.jpeg

(1) 您正在使用 Windows 操作系统。这个片段

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/img/**")
            .addResourceLocations("file://e:/fortest/");
}

.addResourceLocations("file://e:/fortest/");

应该是

.addResourceLocations("file:///E:/fortest/");

(2)

@Configuration
public class MvcConfig implements WebMvcConfigurer {

应该是

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {