Ejemplo n.º 1
0
        public static DownLoadInfo GetDownLoadInfo(string fileName)
        {
            if (downLoadInfos == null)
            {
                downLoadInfos = new Dictionary <string, DownLoadInfo>();
            }

            DownLoadInfo info = null;

            if (downLoadInfos.TryGetValue(fileName, out info))
            {
                return(info);
            }

            info = new DownLoadInfo();
            downLoadInfos.Add(fileName, info);

            return(info);
        }
Ejemplo n.º 2
0
        static void TaskProc(object o)
        {
            HttpListenerContext ctx = (HttpListenerContext)o;

            int    length     = URL.Length;
            string RequestURL = ctx.Request.Url.ToString();
            string fileName   = RequestURL.Substring(length);

            string downloadSizeKey  = "bytes";
            string downloadSpeedKey = "DownLoadSpeed";
            string downloadTypeKey  = "DownLoadType";
            string versionKey       = "Version";
            string osTypeKey        = "OsType";

            DownLoadInfo downLoadInfo = GetDownLoadInfo(fileName);

            downLoadInfo.fileName = fileName;

            string[] allKeys = ctx.Request.Headers.AllKeys;
            for (int i = 0; i < allKeys.Length; i++)
            {
                string   keyValues = ctx.Request.Headers[allKeys[i]];
                string[] context   = keyValues.Split('=');
                if (context.Length <= 0)
                {
                    continue;
                }

                if (context[0] == downloadSizeKey)
                {
                    string[] byteCount = context[1].Split('-');
                    downLoadInfo.DownLoadSize = System.Convert.ToInt32(byteCount[0]);
                }
                else if (context[0] == downloadSpeedKey)
                {
                    downLoadInfo.BitsPerSecond = System.Convert.ToInt32(context[1]);
                }
                else if (context[0] == downloadTypeKey)
                {
                    downLoadInfo.downLoadType = (DownLoadType)Enum.Parse(typeof(DownLoadType), context[1]);
                }
                else if (context[0] == versionKey)
                {
                    downLoadInfo.version = System.Convert.ToInt32(context[1]);
                }
                else if (context[0] == osTypeKey)
                {
                    downLoadInfo.platformType = (ePlatformType)Enum.Parse(typeof(ePlatformType), context[1]);
                }
            }

            downLoadInfo.PrintInfo();

            string fullName = downLoadInfo.GetFullFileName();

            if (!File.Exists(fullName))
            {
                Console.WriteLine(fullName);
                Console.WriteLine("你要下载的文件不存在!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                ctx.Response.StatusCode = 101;
                ctx.Response.Close();
                return;
            }

            ctx.Response.StatusCode = 200; //设置返回给客服端http状态代码

            using (FileStream fs = File.Open(fullName, FileMode.Open))
            {
                if (downLoadInfo.downLoadType != DownLoadType.FileManifest)
                {
                    int number        = 0;
                    int BitsPerSecond = downLoadInfo.BitsPerSecond;

                    Console.WriteLine("文件大小:" + fs.Length);

                    while (true)
                    {
                        int RemainingLength = (int)fs.Length - downLoadInfo.DownLoadSize;
                        if (RemainingLength <= 0)
                        {
                            break;
                        }

                        int downLength = RemainingLength < BitsPerSecond ? RemainingLength : BitsPerSecond;

                        byte[] downBytes = new byte[downLength];

                        fs.Seek(downLoadInfo.DownLoadSize, SeekOrigin.Begin);
                        fs.Read(downBytes, 0, downLength);
                        downLoadInfo.DownLoadSize += downLength;
                        number++;

                        ctx.Response.OutputStream.Write(downBytes, 0, downLength);
                        //Console.WriteLine(string.Format("总共输送了:{0} Bit", downLoadInfo.DownLoadSize));

                        Thread.Sleep(100);
                    }
                    ctx.Response.Close();
                    Console.WriteLine("输送完毕!");
                }
                else
                {
                    byte[] downBytes = new byte[fs.Length];
                    fs.Read(downBytes, 0, downBytes.Length);

                    string context = Encoding.Default.GetString(downBytes);
                    Console.WriteLine("传输数据:" + context);

                    ctx.Response.OutputStream.Write(downBytes, 0, downBytes.Length);
                    ctx.Response.Close();
                }
            }
        }