/** * 视频流读取 * * @param response * @throws Exception */ @RequestMapping(value = "/getvideo/{filetype}/{filename:.+}", method = RequestMethod.GET) public void getvideo(@PathVariable("filename") String filename, @PathVariable("filetype") String filetype, HttpServletResponse response,HttpServletRequest request) throws Exception { //获取的图片绝对路径 String imagePath = location.concat(filetype).concat(File.separator).concat(filename); // 取头参数 String range = request.getHeader("range"); String browser = request.getHeader("User-Agent"); if (browser.indexOf("Firefox") != -1) { // 火狐浏览器 byte[] data = getBytesFromFile(new File(imagePath)); response.setContentType("video/ogg"); response.setContentLength(data.length); response.setHeader("Content-Range", range + Integer.valueOf(data.length - 1)); response.setHeader("Accept-Ranges", "bytes"); response.setHeader("Etag", "W/\"9767057-1323779115364\""); byte[] content = new byte[1024]; BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(data)); OutputStream os = response.getOutputStream(); while (is.read(content) != -1) { os.write(content); } is.close(); os.close(); } else if (browser.indexOf("MSIE") != -1) { // IE9以上浏览器 byte[] data = getBytesFromFile(new File(imagePath)); String diskfilename = "final.mp4"; response.setContentType("video/mpeg"); // response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + diskfilename + "\""); response.setContentLength(data.length); response.setHeader("Content-Range", range + Integer.valueOf(data.length - 1)); response.setHeader("Accept-Ranges", "text/x-dvi"); response.setHeader("Etag", "W/\"9767057-1323779115364\""); byte[] content = new byte[1024]; BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(data)); OutputStream os = response.getOutputStream(); while (is.read(content) != -1) { os.write(content); } is.close(); os.close(); } else if (browser.indexOf("Chrome") != -1) { // 谷歌浏览器 byte[] data = getBytesFromFile(new File(imagePath)); String diskfilename = "final.mp4"; response.setContentType("video/mp4"); // response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + diskfilename + "\""); System.out.println("data.length " + data.length); response.setContentLength(data.length); response.setHeader("Content-Range", range + Integer.valueOf(data.length - 1)); response.setHeader("Accept-Ranges", "bytes"); response.setHeader("Etag", "W/\"9767057-1323779115364\""); byte[] content = new byte[1024]; BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(data)); OutputStream os = response.getOutputStream(); while (is.read(content) != -1) { os.write(content); } is.close(); os.close(); } else if (browser.indexOf("CoreMedia") != -1) { // 其他浏览器 byte[] data = getBytesFromFile(new File(imagePath)); String diskfilename = "final.mp4"; response.setContentType("video/mpeg"); response.setHeader("Content-Disposition", "attachment; filename=\"" + diskfilename + "\""); byte[] content = new byte[1024]; BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(data)); OutputStream os = response.getOutputStream(); while (is.read(content) != -1) { os.write(content); } is.close(); os.close(); } } //读取文件字节 private static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { return null; } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while ((offset < bytes.length) && ((numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; }
关于读取大视频流的接口
最新推荐文章于 2023-03-03 14:22:41 发布