`

java播放二进制 Flv视频

    博客分类:
  • JAVA
 
阅读更多

视屏播放与图片展示展示一样,一般把图片或者视屏放到服务器或者资源服务器,或者CDN上通过URL的方式访问,也可以通过以流的方式输出到客户端浏览器,flv的视屏播放示例如下

 

一.创建一个web项目

1.创建一个servlet

 

/**
 * Servlet implementation class FlvBFServlet
 */
@WebServlet("/FlvBFServlet")
public class FlvBFServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FlvBFServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		
		response.reset();
		response.setCharacterEncoding("utf-8");
		response.setContentType("video/x-flv;charset=utf-8");
		 
		HttpURLConnection connection = null; 
		ByteArrayOutputStream bos = null;
		BufferedInputStream bis = null;
		
		try{
	        // new URL("xx")为flv文件地址,根据实际路径调整
	        URL url = new URL("http://abc.com/download/lesson301.flv");

	        connection = (HttpURLConnection) url.openConnection();
	        // 设置连接超时时间
	        connection.setConnectTimeout(10000);
	        // 设置读超时时间
	        connection.setReadTimeout(10000);
	        // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)
	        connection.connect();

	        // 获得响应状态信息
	        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK){
	           return;
	        }
	        
	        //每次读取的字节数
	        byte[] bytes = new byte[1024];
	        
	        //获得输入流
	        bis =  new BufferedInputStream(connection.getInputStream(), connection.getContentLength());
			
	        //每次读取的字节长度
	        int len;
			
	        //设置输入流
			bos = new ByteArrayOutputStream(bis.available());
			
			while((len = bis.read(bytes, 0, bytes.length)) != -1){
				bos.write(bytes, 0, len);
			}
 			response.getOutputStream().write(bos.toByteArray());
	        response.flushBuffer();
	        
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			connection.disconnect();
		}
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		doGet(request, response);
	}

 

 

2.创建flv.jsp 在根目录

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="640" height="480" id="VideoPlayer" align="middle"> 
<param name="allowScriptAccess" value="*" /> 
<param name="allowFullScreen" value="true" /> 
<param name="movie" value="http://abc.com/download/flvplayer.swf" /> 
<param name"quality" value="high" />
<param name="bgcolor" value="#ffffff" /> 
<embed src="http://abc.com/download/flvplayer.swf" quality="high" bgcolor="#000000" width="640" height="480" name="VideoPlayer" align="middle" allowScriptAccess="*" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"
	flashvars="vcastr_file=http://localhost:8080/YangUp/FlvBFServlet"
 /> 
</object>

</body>
</html>

 

 

    其中:http://mgrtest.roadshowcenter.com/download/flvplayer.swf flv视屏播放器自行下载即可,放到指定的路径,此处也要做相应的调整

       vcastr_file:http://localhost:8080/YangUp/FlvBFServlet 获取输出流的服务器的服务器请求地址

 

3.启动服务访问

http://localhost:8080/YangUp/flv.jsp  就可以看见期望的内容了

 

4.可以对访问资源的请求做些条件限制,比如用户,header中的Referer 以及请求ULR的时效限制等

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics