1
using
System;
2
using
System.IO;
3
using
System.Web;
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
7
/// Summary description for FLVStreaming
8
/// </summary>
9
public
class
FLVStreaming : IHttpHandler
10
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
11
private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009"); //"FLV\x1\x1\0\0\0\x9\0\0\0\x9"
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public FLVStreaming()
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
}
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public void ProcessRequest(HttpContext context)
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
try
20![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
int pos;
22
int length;
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
// Check start parameter if present
25
string filename = Path.GetFileName(context.Request.FilePath);
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
using (FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read))
28![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
string qs = context.Request.Params["start"];
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
if (string.IsNullOrEmpty(qs))
32![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
pos = 0;
34
length = Convert.ToInt32(fs.Length);
35
}
36
else
37![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
pos = Convert.ToInt32(qs);
39
length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;
40
}
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
// Add HTTP header stuff: cache, content type and length
43
context.Response.Cache.SetCacheability(HttpCacheability.Public);
44
context.Response.Cache.SetLastModified(DateTime.Now);
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
context.Response.AppendHeader("Content-Type", "video/x-flv");
47
context.Response.AppendHeader("Content-Length", length.ToString());
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
// Append FLV header when sending partial file
50
if (pos > 0)
51![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
53
fs.Position = pos;
54
}
55![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
// Read buffer and write stream to the response stream
57
const int buffersize = 16384;
58
byte[] buffer = new byte[buffersize];
59
60
int count = fs.Read(buffer, 0, buffersize);
61
while (count > 0)
62![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
if (context.Response.IsClientConnected)
64![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
context.Response.OutputStream.Write(buffer, 0, count);
66
count = fs.Read(buffer, 0, buffersize);
67
}
68
else
69![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
count = -1;
71
}
72
}
73
}
74
}
75
catch (Exception ex)
76![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
System.Diagnostics.Debug.WriteLine(ex.ToString());
78
}
79
}
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
public bool IsReusable
82![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return true; }
84
}
85![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
private static byte[] HexToByte(string hexString)
87![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
byte[] returnBytes = new byte[hexString.Length / 2];
89
for (int i = 0; i < returnBytes.Length; i++)
90
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
91
return returnBytes;
92
}
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
}
95
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
修改配置文件,运行页面即可调出视频,
转载于:https://www.cnblogs.com/appleseed/archive/2008/08/07/1263146.html