我打算从服务器读取一大块字节到我的客户端说client1,然后将该字节写入另一个属于client2的流。所以基本上Client2直接从client1流式传输,而client1又从Web服务器流式传输。 我想重复这个过程,直到下载完成。
我有一个代码片段用于使用本地文件执行此操作,是否可以使用托管在Web服务器上的文件?
private void GetLocalFile(HttpRequest request) //Request from client2
{
HttpResponse response = new HttpResponse(request);
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
long count;
if (request.Headers.ContainsKey("range"))
{
string[] range = request.Headers["range"].Split('=').Last().Split('-').Select(a => a.Trim()).ToArray();
long end, start = long.Parse(range[0]);
if (start < 0)
start += fs.Length;
if (range.Length < 2 || !long.TryParse(range[1], out end))
end = fs.Length - 1;
count = end - start + 1;
fs.Position = start;
response.SetStateCode(206);
response.AddHreader("Content-Range", string.Format("bytes {0}-{1}/{2}", start, end, fs.Length));
}
else
{
count = fs.Length;
}
response.AddHreader(HttpHeader.ContentLength, count.ToString());
response.AddHreader(HttpHeader.ContentType, mime.ToString());
response.AddHreader(HttpHeader.AcceptRanges, "bytes");
response.AddHreader("TransferMode.DLNA.ORG", "Streaming");
response.AddHreader("ContentFeatures.DLNA.ORG", fileFeature);
response.SendHeaders();
if (request.Method == "GET")
{
int readed;
byte[] buffer = new byte[4096];
Stream responseStream = request.Socket.GetStream();
while ((readed = fs.Read(buffer, 0, buffer.Length)) > 0)
{
readed = (int)Math.Min(readed, count);
responseStream.Write(buffer, 0, readed);
count -= readed;
if (count <= 0)
break;
}
}
}
}