【SpringBoot】配置addResourceHandler访问静态资源问题

【SpringBoot】配置addResourceHandler访问静态资源问题,第1张

【SpringBoot】配置addResourceHandler访问静态资源问题

配置application.yml

file:
  path:
    windows: D:desktophomeidea_homezhzfpic
    linux: /Users/lhk/Desktop/zhzf/pic/

配置WebMvcConfig

windowsFilePath:windows系统本地静态资源的真实地址(对应application.yml中的windows)

linuxFilePath:linux系统本地静态资源的真实地址(对应application.yml中的linux)

通过registry.addResourceHandler("/img @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String os = System.getProperty("os.name"); if (os.toLowerCase().startsWith("win")) { //如果是Windows系统 registry.addResourceHandler("/img/**").addResourceLocations("file:"+windowsFilePath); }else{ //linux和mac系统 registry.addResourceHandler("/img/**").addResourceLocations("file:"+linuxFilePath+"/"); } }

测试:

本地图片地址:/Users/lhk/Desktop/zhzf/pic/123.jpg

数据库存储地址: http://ip地址:端口号/img/123.jpg

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5695755.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存