springboot2.0知道图片或视频存放路径,怎么返回图片或视频的展示或播放url

您所在的位置:网站首页 播放波涛的图片 springboot2.0知道图片或视频存放路径,怎么返回图片或视频的展示或播放url

springboot2.0知道图片或视频存放路径,怎么返回图片或视频的展示或播放url

2023-08-13 03:59| 来源: 网络整理| 查看: 265

1.先上传视频或图片,并把路径保存在数据库里(怎么上传就不说了,网上很多)。

我的是把视频存放到F盘fileUpload文件夹下:

mysql数据库中保存地址:

2.配置springboot。

  2.1、配置application.properties文件,添加配置指定上传路径,比如我的配置字段为:cbs.imagesPath=file:/F:/fileUpload/

2.2、添加配置类:

import org.slf4j.LoggerFactory; 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.WebMvcConfigurerAdapter; //上传配置类 //图片放到/F:/fileUpload/后,从磁盘读取的图片数据scr将会变成images/picturename.jpg的格式 @Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { @Value("${cbs.imagesPath}") private String mImagesPath; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if(mImagesPath.equals("") || mImagesPath.equals("${cbs.imagesPath}")){ String imagesPath = WebAppConfig.class.getClassLoader().getResource("").getPath(); System.out.print("1.上传配置类imagesPath=="+imagesPath+"\n"); if(imagesPath.indexOf(".jar")>0){ imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar")); }else if(imagesPath.indexOf("classes")>0){ imagesPath = "file:"+imagesPath.substring(0, imagesPath.indexOf("classes")); } imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"))+"/images/"; mImagesPath = imagesPath; } LoggerFactory.getLogger(WebAppConfig.class).info("imagesPath="+mImagesPath); registry.addResourceHandler("/images/**").addResourceLocations(mImagesPath); // TODO Auto-generated method stub System.out.print("2.上传配置类mImagesPath=="+mImagesPath+"\n"); super.addResourceHandlers(registry); } }

3.读取数据库中的图片或视频路径,返回url:

例如:数据库中视频保存路径:F:/fileUpload/20180808105540_aa.mp4

读取到这个路径,拼接成:

http://localhost:8088/images/20180808105540_aa.mp4

直接放到浏览器就可以播放了:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3