关于springboot使用WebMvcConfigurer映射本地图片失败的问题,求助!

北北啊我是 2020-09-04 10:43:13
一个springboot项目, 没有图片服务器, 想通过 WebMvcConfigurer 的 addResourceHandler\ addResourceLocations方法将本地图片进行映射, 然后路径访问, 但一直不成功, 如图, 麻烦高人给看下


不知跟springboot版本有关吗? 我的好像比较老 1.4.4.RELEASE ....
...全文
3627 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
北北啊我是 2020-09-05
  • 打赏
  • 举报
回复
引用 9 楼 pinoco 的回复:
[quote=引用 5 楼 pinoco 的回复:]我按着他的写了一下没什么问题,我访问到了,没什么问题 配置类代码,我基本没这么改,一样用的 / 你访问路径为什么是10.0.0.110:8090/img/图片名,你这个img是在yml文件中配置的吗?如果是应该是没有什么问题的
2.1.5的,直接pom文件下载个jar包也不麻烦。我后面也尝试改动了一些,没有出现404,除非路径对应不上[/quote] 嗯 我新建了个版本高的boot项目 也可以访问到图了.. 可能就是版本问题了..
北北啊我是 2020-09-05
  • 打赏
  • 举报
回复
引用 8 楼 shuangmu9768 的回复:
亲测可以的
感谢回复 我新建了boot版本高的项目 也是可以了.. 可能就是版本问题...
明白畅达 2020-09-04
  • 打赏
  • 举报
回复
引用 5 楼 pinoco 的回复:
我按着他的写了一下没什么问题,我访问到了,没什么问题 配置类代码,我基本没这么改,一样用的 / 你访问路径为什么是10.0.0.110:8090/img/图片名,你这个img是在yml文件中配置的吗?如果是应该是没有什么问题的
2.1.5的,直接pom文件下载个jar包也不麻烦。我后面也尝试改动了一些,没有出现404,除非路径对应不上
shuangmu9768 2020-09-04
  • 打赏
  • 举报
回复
亲测可以的
shuangmu9768 2020-09-04
  • 打赏
  • 举报
回复
package cn.lml.lession.config; import java.util.Date; import java.util.TimeZone; import javax.annotation.PostConstruct; import javax.servlet.MultipartConfigElement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.GenericConversionService; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.util.Assert; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import cn.lml.lession.util.DateUtils; import cn.lml.tools.encrypt.MessageDigestUtils; @Configuration public class ApplicationConfig implements WebMvcConfigurer { @Autowired private RequestMappingHandlerAdapter handlerAdapter; @PostConstruct public void addConversionConfig() { ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter .getWebBindingInitializer(); if (initializer.getConversionService() != null) { GenericConversionService genericConversionService = (GenericConversionService) initializer .getConversionService(); genericConversionService.addConverter(new StringToDateConverter()); } TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); } public class StringToDateConverter implements Converter<String, Date> { @Override public Date convert(String dateString) { Assert.hasText(dateString, "Null or emtpy date string"); Date date = DateUtils.parseDate(dateString); if (date == null) { String errMsg = String.format("Failed to convert [%s] to [%s] for value '%s'", String.class.toString(), Date.class.toString(), dateString); throw new IllegalArgumentException(errMsg); } return date; } } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("forward:/index.html"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } /** * 重写父类提供的跨域请求处理的接口 */ @Override public void addCorsMappings(CorsRegistry registry) { // 添加映射路径 registry.addMapping("/**") // 放行哪些原始域 .allowedOrigins("*") // 是否发送Cookie信息 .allowCredentials(true) // 放行哪些原始域(请求方式) .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH") // 放行哪些原始域(头部信息) .allowedHeaders("*") // 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息) .exposedHeaders("Header1", "Header2"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //registry.addResourceHandler("/MyResources/**").addResourceLocations("classpath:/static/"); //registry.addResourceHandler("/MyResources/**").addResourceLocations("file:E:/static/"); } /** * 限制上传文件大小 */ /* * @Bean public MultipartConfigElement multipartConfigElement(){ * MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 10m * 可以使用读取配置 factory.setMaxFileSize("10240KB"); //KB,MB /// 设置总上传数据总大小 50m * factory.setMaxRequestSize("512000KB"); return * factory.createMultipartConfig(); } */ @Bean public PasswordEncoder passwordEncoder() { return new PasswordEncoder() { MessageDigestUtils md5=MessageDigestUtils.newInstance(MessageDigestUtils.EncryptType.MD5); @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { return encodedPassword.equals(md5.getEncryptString(rawPassword.toString())); } @Override public String encode(CharSequence rawPassword) { return md5.getEncryptString(rawPassword.toString()); } }; } }
北北啊我是 2020-09-04
  • 打赏
  • 举报
回复
引用 5 楼 pinoco 的回复:
我按着他的写了一下没什么问题,我访问到了,没什么问题 配置类代码,我基本没这么改,一样用的 / 你访问路径为什么是10.0.0.110:8090/img/图片名,你这个img是在yml文件中配置的吗?如果是应该是没有什么问题的
谢谢回复啊 你的springboot版本多少呢? 我打算换个版本试试...
明白畅达 2020-09-04
  • 打赏
  • 举报
回复
我按着他的写了一下没什么问题,我访问到了,没什么问题 配置类代码,我基本没这么改,一样用的 / 你访问路径为什么是10.0.0.110:8090/img/图片名,你这个img是在yml文件中配置的吗?如果是应该是没有什么问题的
北北啊我是 2020-09-04
  • 打赏
  • 举报
回复
引用 3 楼 浪费七年时间 的回复:
写法没什么问题,我估计是你这个项目有context-path,但你地址栏没有(host:port/context-path/img/**)
感谢回复 context-path写过 静态资源路径也写过 但是现在注释掉了呀.. 我很费解.. 另外 会是springboot版本的问题吗? ?
浪费七年时间 2020-09-04
  • 打赏
  • 举报
回复
写法没什么问题,我估计是你这个项目有context-path,但你地址栏没有(host:port/context-path/img/**)
北北啊我是 2020-09-04
  • 打赏
  • 举报
回复
引用 1 楼 KeepSayingNo 的回复:
你这个图片静态资源要放在classpath下面吧,你放到你的项目的resource文件下下,然后修改下代码 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
感谢回复啊 我的意思是不把图片放到项目里, 只是访问我本地的图片. 就类似这篇文章, https://blog.csdn.net/weixin_41413137/article/details/108269164#comments_13178827
KeepSayingNo 2020-09-04
  • 打赏
  • 举报
回复
你这个图片静态资源要放在classpath下面吧,你放到你的项目的resource文件下下,然后修改下代码 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧