C# 一行代碼使Http下載功能變爲Ftp下載

Http下載代碼

        public void HttpDownloadProcess(long lStartPos, FileStream fs)
        {
            do
            {
                try
                {
                    if (FileSize == 0)
                    {
                        FileSize = GetFileContentLength(Url);
                    }
                    if (FileSize != 0 && FileSize == lStartPos)
                    {
                        //下載完成
                        fs.Close();
                        return;
                    }

                    var request = (HttpWebRequest)WebRequest.Create(Url);
                    request.ReadWriteTimeout = ReadWriteTimeOut;
                    request.Timeout = TimeOutWait;
                    if (lStartPos > 0)
                        request.AddRange((int)lStartPos);//設置Range值,斷點續傳

                    //向服務器請求,獲得服務器迴應數據流
                    WebResponse respone = request.GetResponse();
                    this.FileSize = respone.ContentLength + lStartPos;
                    this.FileDownloadedSize = lStartPos;

                    Stream ns = respone.GetResponseStream();

                    byte[] nbytes = new byte[bytebuff];
                    int nReadSize = -1;
                    while (nReadSize > 0 || nReadSize == -1)
                    {
                        nReadSize = ns.Read(nbytes, 0, bytebuff);
                        fs.Write(nbytes, 0, nReadSize);
                        fs.Flush();
                        FileDownloadedSize += nReadSize;
                    }
                    ns.Close();
                    fs.Close();
                    if (FileDownloadedSize != FileSize)//文件長度不等於下載長度,下載出錯
                    {
                        Exception exception = new Exception("下載文件檢驗錯誤");
                        throw exception;
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                    DownloadingCompleted(this, DownloadStatus.Completed);
                    TryCount = 0;
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                    fs.Close();
                }
            }
            while (CheckErrorTryCounts() && Status == DownloadStatus.Downloading);
            if (request != null)
            {
                request.Abort();
            }
        }

Ftp下載代碼

        public void FtpDownloadProcess(long lStartPos, FileStream fs)
        {
            FtpWebRequest request = null;
            do
            {
                try
                {
                    if (FileSize == 0)
                    {
                        FileSize = GetFileContentLength(Url);
                    }
                    if (FileSize != 0 && FileSize == lStartPos)
                    {
                        //下載完成
                        fs.Close();
                        return;
                    }

                    request = (FtpWebRequest)FtpWebRequest.Create(Url);
                    request.ReadWriteTimeout = ReadWriteTimeOut;
                    request.Timeout = TimeOutWait;

                    //向服務器請求,獲得服務器迴應數據流
                    WebResponse respone = request.GetResponse();
                    this.FileSize = respone.ContentLength + lStartPos;
                    this.FileDownloadedSize = lStartPos;

                    Stream ns = respone.GetResponseStream();

                    byte[] nbytes = new byte[bytebuff];
                    int nReadSize = -1;
                    while (nReadSize > 0 || nReadSize == -1)
                    {
                        nReadSize = ns.Read(nbytes, 0, bytebuff);
                        fs.Write(nbytes, 0, nReadSize);
                        fs.Flush();
                        FileDownloadedSize += nReadSize;
                    }
                    ns.Close();
                    fs.Close();
                    if (FileDownloadedSize != FileSize)//文件長度不等於下載長度,下載出錯
                    {
                        Exception exception = new Exception("下載文件檢驗錯誤");
                        throw exception;
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                    DownloadingCompleted(this, DownloadStatus.Completed);
                    TryCount = 0;
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                    fs.Close();
                }
            }
            while (CheckErrorTryCounts() && Status == DownloadStatus.Downloading);
            if (request != null)
            {
                request.Abort();
            }
        }

.Net的好寫真的不是吹噓啊哈哈哈

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章