前臺播放本地絕對路徑視頻

前臺播放本地絕對路徑視頻

jsp前臺

<video id="img" muted="muted" autoplay="autoplay" loop controls="controls"
 	src="/hospital/video">
</video>

java後臺

@Controller
@RequestMapping("/hospital")
public class EmployeeController {
    @RequestMapping("/main")
    public String index(){
        return "main";
    }

    /**
     * 加載視頻文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("video")
    public void getVideoPath(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            //創建輸入流
            fis = new FileInputStream("d:/log/tt.mp4");
            //os用於輸出字符流數據或者二進制的字節流數據都可以。
            os = response.getOutputStream();
            int count = 0;
            //開始讀,每次讀1024*8
            byte[] buffer = new byte[1024 * 8];
            //將filepath的文件放入buffer作爲內存緩衝區並獲得可讀的長度len
            while ((count = fis.read(buffer)) != -1) {
                //寫到OutputStream中,每次都是從buffer內存處的0偏移開始寫,每次寫count字節
                os.write(buffer, 0, count);
                os.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章