【发布时间】: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