更新:
我按照 M. Deinum 的建議使用 ClassPathResource 讓它作業。我還洗掉了我的 WebMvcConfig,但我不確定這是否與問題有關。
@RequestMapping(value = "/image", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] getImage() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("static/images/user-default.png");
InputStream in = classPathResource.getInputStream();
return IOUtils.toByteArray(in);
}
我正在使用 Spring MVC 架構和 Spring Security 進行 Spring Boot 專案。我無法確定我錯誤配置 MVC 配置的位置,我遇到了控制器無法找到我指定的影像的問題。
這是我的控制器,它提供影像:
@Controller
public class ImageController {
@Autowired
private ServletContext servletContext;
@RequestMapping(value = "/image", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] getImage() throws IOException {
InputStream in = servletContext.getResourceAsStream("/images/user-default.png");
return IOUtils.toByteArray(in);
}
}
通過除錯,我發現InputStream導致null。但是,我實際上可以通過Thymeleaf 模板在我的瀏覽器上加載影像:
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<!--omitted for brevity-->
<img th:src="@{/images/quiz-academic.png}"/> <!--This WORKS, image is VISIBLE in the browser-->
<!--omitted for brevity-->
</html>
或者通過直接路徑訪問它:
http://localhost:8080/images/user-default.png (This WORKS, image is VISIBLE in the browser)
I'm confused as to why my controller is not able to pick up the image yet the two above methods (Thymeleaf, direct path) could do that without any problem.
Here's my MVC Configuration:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/resources/", "classpath:/static/", "classpath:/images/");
}
}
Here's my Security configuration:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/error");
http.headers().frameOptions().disable();
http.authorizeRequests()
.antMatchers("/admin/**").access("hasAnyRole('ROLE_ADMIN')")
.antMatchers("/user").access("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/images/**").permitAll()
.antMatchers("/image").permitAll()
.antMatchers("/").permitAll()
.anyRequest().authenticated();
/* login/logout configuration omitted for brevity */
}
}
And my project directories:
src/main/java/com/kjnts/springwebapp/config/WebSecurityConfig.java
src/main/java/com/kjnts/springwebapp/config/WebMvcConfig.java
src/main/java/com/kjnts/springwebapp/controller/ImageController.java
src/main/resources/static/images/user-default.png
src/main/resources/templates/user-profile.html
If you are wondering, I don't use XML for Spring configuration.
Thank you.
uj5u.com熱心網友回復:
想我應該發布一個答案,以便我可以將其標記為已回答。
我按照 M. Deinum 的建議使用 ClassPathResource 讓它作業。我還洗掉了我的 WebMvcConfig,但我不確定這是否與問題有關。
@RequestMapping(value = "/image", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] getImage() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("static/images/user-default.png");
InputStream in = classPathResource.getInputStream();
return IOUtils.toByteArray(in);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360464.html
標籤:spring spring-boot spring-mvc
