Exemple #1
0
 public void ReleaseRessources()
 {
     if (this.FStream != null)
     {
         this.FStream.Close();
         try
         {
             FStream.Dispose();
         }
         catch (Exception ex)
         {
             string message = ex.Message;
         }
     }
 }
        public static bool Download(string sourceFile, string desFile)
        {
            bool       flag      = false;
            long       SPosition = 0;
            FileStream FStream   = null;
            Stream     myStream  = null;
            string     fileName  = sourceFile.Substring(sourceFile.LastIndexOf(@"/") + 1);

            if (desFile.EndsWith("\\"))
            {
                desFile = desFile + fileName;
            }
            else
            {
                desFile = desFile + "\\" + fileName;
            }
            try
            {
                long serverFileLength = 0;
                //判断要下载的文件夹是否存在
                if (File.Exists(desFile))
                {
                    //打开上次下载的文件
                    FStream = File.OpenWrite(desFile);
                    //获取已经下载的长度
                    SPosition        = FStream.Length;
                    serverFileLength = GetHttpLength(sourceFile);
                    if (SPosition == serverFileLength)
                    {//文件是完整的,直接结束下载任务
                        return(true);
                    }
                    FStream.Seek(SPosition, SeekOrigin.Current);
                }
                else
                {
                    //文件不保存创建一个文件
                    FStream   = new FileStream(desFile, FileMode.Create);
                    SPosition = 0;
                }

                //打开网络连接
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(sourceFile);

                if (SPosition > 0)
                {
                    myRequest.AddRange(SPosition);             //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                myStream = myRequest.GetResponse().GetResponseStream();

                //定义一个字节数据
                byte[] btContent = new byte[512];
                int    intSize   = 0;
                intSize = myStream.Read(btContent, 0, 512);
                while (intSize > 0)
                {
                    FStream.Write(btContent, 0, intSize);
                    intSize = myStream.Read(btContent, 0, 512);
                }
                flag = true;        //返回true下载成功
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("下载文件时异常:" + ex.Message + ex.StackTrace);
            }
            finally
            {
                //关闭流
                if (myStream != null)
                {
                    myStream.Close();
                    myStream.Dispose();
                }
                if (FStream != null)
                {
                    FStream.Close();
                    FStream.Dispose();
                }
            }
            return(flag);
        }
Exemple #3
0
        public bool Start()
        {
            if (SavePath == "" || Url == "")
            {
                return(false);
            }
            if (!SavePath.EndsWith("\\"))
            {
                SavePath += "\\";
            }

            bool       flag = false;
            long       SPosition;
            FileStream FStream  = null;
            Stream     myStream = null;
            String     desFile  = SavePath + SaveFile + Suffix;

            try
            {
                //判断要下载的文件夹是否存在
                if (File.Exists(desFile))
                {
                    //打开上次下载的文件
                    FStream = File.OpenWrite(desFile);
                    //获取已经下载的长度
                    SPosition = FStream.Length;
                    long serverFileLength = GetHttpLength(Url);
                    if (SPosition == serverFileLength)
                    {//文件是完整的,直接结束下载任务
                        return(true);
                    }
                    FStream.Seek(SPosition, SeekOrigin.Current);
                }
                else
                {
                    //文件不保存创建一个文件
                    FStream   = new FileStream(desFile, FileMode.Create);
                    SPosition = 0;
                }
                //打开网络连接
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
                if (SPosition > 0)
                {
                    myRequest.AddRange(SPosition);             //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                myStream = myRequest.GetResponse().GetResponseStream();
                //定义一个字节数据
                byte[] btContent = new byte[512];
                int    intSize   = 0;
                intSize = myStream.Read(btContent, 0, 512);
                while (intSize > 0)
                {
                    FStream.Write(btContent, 0, intSize);
                    intSize = myStream.Read(btContent, 0, 512);
                }
                flag = true;        //返回true下载成功
            }
            catch (Exception ex)
            {
                //下载文件时异常
            }
            finally
            {
                //关闭流
                if (myStream != null)
                {
                    myStream.Close();
                    myStream.Dispose();
                }
                if (FStream != null)
                {
                    FStream.Close();
                    FStream.Dispose();
                }
            }
            if (flag)
            {
                File.Move(desFile, SavePath + SaveFile);
            }
            return(flag);
        }