欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

Java

當(dāng)前位置:主頁(yè) > 軟件編程 > Java >

SpringBoot靜態(tài)視頻實(shí)時(shí)播放的實(shí)現(xiàn)代碼

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Java|點(diǎn)擊: 次

問(wèn)題描述

Spring Boot API 定義 GET 請(qǐng)求 API , 返回視頻流。前端通過(guò) <video> 標(biāo)簽加載并播放視頻,效果是必須等整個(gè)視頻資源全部加載到瀏覽器才能播放,而且 <video> 標(biāo)簽?zāi)J(rèn)的進(jìn)度條無(wú)法控制視頻的播放。最終希望的效果是視頻流邊加載邊播放,且播放器的控制正常使用。

解決方法

Spring Framework 文件請(qǐng)求處理

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Path;

@Component
public class NonStaticResourceHttpRequestHandler extends ResourceHttpRequestHandler {

  public final static String ATTR_FILE = "NON-STATIC-FILE";

  @Override
  protected Resource getResource(HttpServletRequest request) {
    final Path filePath = (Path) request.getAttribute(ATTR_FILE);
    return new FileSystemResource(filePath);
  }

}

Controller 層

@RestController
@AllArgsConstructor
public class FileRestController {

  private final NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;

  /**
   * 預(yù)覽視頻文件, 支持 byte-range 請(qǐng)求
   */
  @GetMapping("/video")
  public void videoPreview(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String path = request.getParameter("path");
    Path filePath = Paths.get(path);
    if (Files.exists(filePath)) {
      String mimeType = Files.probeContentType(filePath);
      if (!StringUtils.isEmpty(mimeType)) {
        response.setContentType(mimeType);
      }
      request.setAttribute(NonStaticResourceHttpRequestHandler.ATTR_FILE, filePath);
      nonStaticResourceHttpRequestHandler.handleRequest(request, response);
    } else {
      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
      response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
    }
  }

}

相關(guān)資料

How do I return a video with Spring MVC so that it can be navigated using the html5 tag?

https://stackoverflow.com/questions/3303029/http-range-header

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Java Linkedlist原理及實(shí)例詳解

欄    目:Java

下一篇:Java里的static在Kotlin里如何實(shí)現(xiàn)

本文標(biāo)題:SpringBoot靜態(tài)視頻實(shí)時(shí)播放的實(shí)現(xiàn)代碼

本文地址:http://mengdiqiu.com.cn/a1/Java/8749.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有