SpringBoot靜態(tài)視頻實(shí)時(shí)播放的實(shí)現(xià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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java圖形界面之JFrame,JLabel,JButton詳解
這篇文章主要介紹了Java圖形界面之JFrame、JLabel、JButton詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09功能強(qiáng)大的TraceId?搭配?ELK使用詳解
這篇文章主要為大家介紹了功能強(qiáng)大的TraceId?搭配?ELK使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09springboot中使用jpa下hibernate的ddl-auto方式
這篇文章主要介紹了springboot中使用jpa下hibernate的ddl-auto方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02k8s部署java項(xiàng)目的實(shí)現(xiàn)
本文主要介紹了k8s部署java項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12mybatis條件語(yǔ)句中帶數(shù)組參數(shù)的處理
這篇文章主要介紹了mybatis條件語(yǔ)句中帶數(shù)組參數(shù)的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09springboot整合websocket實(shí)現(xiàn)群聊思路代碼詳解
通過(guò)springboot引入websocket,實(shí)現(xiàn)群聊,通過(guò)在線websocket測(cè)試進(jìn)行展示。本文重點(diǎn)給大家介紹springboot整合websocket實(shí)現(xiàn)群聊功能,代碼超級(jí)簡(jiǎn)單,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2021-05-05