ResourceHttpRequestHandler: ресурс не найден / изображение из локальных каталогов - PullRequest
0 голосов
/ 31 декабря 2018

Я использую 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

Iполучил этот журнал

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

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

сначала Iполучил страницу whitelabel, теперь я только что получил «незагруженное» изображение.Что я делаю не так, и какой лучший способ обработки локальных ресурсов, если не это для меня?и да, у меня есть image.jpeg в /e:/fortest

1 Ответ

0 голосов
/ 31 декабря 2018

(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 {
...