【问题标题】:ResourceHttpRequestHandler : resource not found / image from local directoriesResourceHttpRequestHandler :找不到资源/本地目录中的图像
【发布时间】:2019-05-27 16:58:10
【问题描述】:

我正在使用 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 属性并将其硬编码在类中

@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 页面,现在我只是收到了“未加载”的图片。我做错了什么,如果不适合我,还有什么更好的方法来处理本地资源?是的,我在/e:/fortest 中有image.jpeg

【问题讨论】:

    标签: spring spring-boot resources static-content


    【解决方案1】:

    (1) 您使用的是 Windows 操作系统。这个sn-p

    @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 {
    

    【讨论】:

    • 谢谢,但我仍然对属性“upload.path”有疑问。有什么解决方案吗?只是不想在类内硬编码将其更改为upload.path=/E:/fortest
    • 再次为这个问题感到非常抱歉。忘记了表达式 $ like @Value("${upload.path}") private String uploadPath; 我得到了答案非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2012-01-12
    相关资源
    最近更新 更多