從文件服務器讀取音視頻文件,以流的方式傳給前臺,並可以播放視頻。html
作了一個demo,用html5的video,audio標籤實現。前端
後臺實現代碼:html5
@GetMapping(value = "/getVideos") public String getVideos(HttpServletRequest request, HttpServletResponse response) { try { FileInputStream fis = null; OutputStream os = null ; fis = new FileInputStream("C:\\Users\\zhangxin\\Desktop\\douyin.mp4"); int size = fis.available(); // 獲得文件大小 byte data[] = new byte[size]; fis.read(data); // 讀數據 fis.close(); fis = null; response.setContentType("video/mp4"); // 設置返回的文件類型 os = response.getOutputStream(); os.write(data); os.flush(); os.close(); os = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
前端實現代碼:服務器
<video width="1120" height="540" controls="controls" id="video" preload="auto" > <source src="getVideos" type="video/mp4"> </video>