springboot通过接口访问html,springboot使用HTML播放接口返回的视频流

20200817记录两种接口返回视频流的方式,HTML中直接实现视频流的播放,

因为资源有限,所以我直接使用下载好的一个视频放在项目下测试

springboot通过接口访问html,springboot使用HTML播放接口返回的视频流_第1张图片

代码中使用的时候换成绝对路径也是一样的效果。

一.pom.xml

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter

org.springframework

spring-test

org.springframework.boot

spring-boot-starter-web

二.application.properties

server.port=8082

server.context-path=/demo

spring.mvc.view.prefix=classpath:/templates/

spring.mvc.view.suffix=.html

三.编写接口.返回视频流

1 packagecom.web.boot.controller;2

3 importorg.apache.commons.io.IOUtils;4 importorg.springframework.http.HttpStatus;5 importorg.springframework.http.MediaType;6 importorg.springframework.stereotype.Controller;7 importorg.springframework.web.bind.annotation.RequestMapping;8 importorg.springframework.web.bind.annotation.RequestMethod;9 importorg.springframework.web.bind.annotation.ResponseBody;10 importjava.io.File;11 importjava.io.FileInputStream;12 importjava.io.InputStream;13 importjava.io.OutputStream;14 importjavax.servlet.http.HttpServletRequest;15 importjavax.servlet.http.HttpServletResponse;16

17 @RequestMapping(value = "redio")18 @Controller19 public classRedioController {20 /**

21 * video 视频流一22 *23 *@paramrequest24 *@paramresponse25 */

26 @RequestMapping(value = "/getVido", method =RequestMethod.GET)27 @ResponseBody28 public voidgetVido(HttpServletRequest request, HttpServletResponse response) {29 String file = "Av706.mp4";30 try{31 FileInputStream inputStream = newFileInputStream(file);32 byte[] data = new byte[inputStream.available()];33 inputStream.read(data);34 String diskfilename = "final.mp4";35 response.setContentType("video/mp4");36 response.setHeader("Content-Disposition", "attachment; filename="" + diskfilename + """);37 System.out.println("data.length " +data.length);38 response.setContentLength(data.length);39 response.setHeader("Content-Range", "" + Integer.valueOf(data.length - 1));40 response.setHeader("Accept-Ranges", "bytes");41 response.setHeader("Etag", "W/"9767057-1323779115364"");42 OutputStream os =response.getOutputStream();43

44 os.write(data);45 //先声明的流后关掉!

46 os.flush();47 os.close();48 inputStream.close();49

50 } catch(Exception e) {51

52 }53 }54 /**

55 * video 视频流二56 *57 * IOUtils is available in Apache commons io58 */

59 @RequestMapping(value = "/preview2", method =RequestMethod.GET)60 @ResponseBody61 public voidgetPreview2( HttpServletResponse response) {62 try{63

64 File file = new File("Av706.mp4");65 response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);66 response.setHeader("Content-Disposition", "attachment; filename="+file.getName().replace(" ", "_"));67 InputStream iStream = newFileInputStream(file);68 IOUtils.copy(iStream, response.getOutputStream());69 response.flushBuffer();70 } catch(java.nio.file.NoSuchFileException e) {71 response.setStatus(HttpStatus.NOT_FOUND.value());72 } catch(Exception e) {73 response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());74 }75 }76 //跳转html

77 @RequestMapping(value ="/hello")78 publicString index() {79 return "hello";80 }81 }

四.编写 hello.html

springboot通过接口访问html,springboot使用HTML播放接口返回的视频流_第2张图片

Insert title here

springboot访问第一个html页面

提交

五.效果

springboot通过接口访问html,springboot使用HTML播放接口返回的视频流_第3张图片

你可能感兴趣的:(springboot通过接口访问html,springboot使用HTML播放接口返回的视频流)