Ejemplo n.º 1
0
        /// <summary>
        /// 下载文件方法
        /// </summary>
        /// <param name="localFilePath"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool Download(string localFilePath, string url)
        {
            bool flag = false;
            //打开上次下载的文件
            long SPosition = 0;
            //实例化流对象
            FileStream FStream;
            //判断要下载的文件夹是否存在
            string ParentDirectory = Path.GetDirectoryName(localFilePath);

            if (!Directory.Exists(ParentDirectory))
            {
                Directory.CreateDirectory(ParentDirectory);
            }
            if (File.Exists(localFilePath))
            {
                //打开要下载的文件
                FStream = File.OpenWrite(localFilePath);
                //获取已经下载的长度
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);
            }
            else
            {
                //文件不保存创建一个文件
                FStream   = new FileStream(localFilePath, FileMode.Create);
                SPosition = 0;
            }
            try
            {
                //打开网络连接
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                long           length    = myRequest.GetResponse().ContentLength;
                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);  //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                Stream 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);
                }
                //关闭流
                FStream.Close();
                myStream.Close();
                flag = true;        //返回true下载成功
            }
            catch (Exception e)
            {
                FStream.Close();
                flag = false;       //返回false下载失败
            }
            return(flag);
        }
Ejemplo n.º 2
0
        private void CheckDown()
        {
            CheckB p = IsCheckTable.Find(x => x.Checkd == false);

            if (p == null)
            {
                BufferFormatV2 buff = new BufferFormatV2((int)PackType.DownClose);
                buff.AddItem(down.DownKey);
                SocketManager.Send(buff.Finish());

                FStream.Close();



                this.BeginInvoke(new EventHandler((a, b) =>
                {
                    this.Text         = DownDir + "--下载完毕";
                    this.button1.Text = "完成";
                    MessageBox.Show(Text);
                }));
            }
            else
            {
                BufferFormatV2 buff = new BufferFormatV2((int)PackType.ReBytes);
                buff.AddItem(down.DownKey);
                buff.AddItem(p.StartPostion);
                buff.AddItem(p.Size);
                SocketManager.Send(buff.Finish());
            }
        }
        //移入ZIP类
        /// <summary>
        /// 从指定站点下载文件,并保存在指定位置,开始位置,完成度(用于ProgressBar使用)
        /// </summary>
        /// <param name="strFileName">本地物理路径</param>
        /// <param name="strUrl">目标Url</param>
        public static void DownloadFile(string strFileName, string strUrl, int begin, string flag)
        {
            if (SafeSC.FileNameCheck(strFileName))
            {
                throw new Exception(strFileName + "取消保存");
            }
            //已完成的,1%长度
            int  CompletedLength = 0;
            long percent = 0; progStatus = "0"; string temp = "0";
            long sPosstion = 0; //磁盘现盘文件的长度
                                //long count = 0;// count += sPosstion,从指定位置开始写入字节
            FileStream FStream;

            if (File.Exists(strFileName))                    //如果文件存在
            {
                FStream   = File.OpenWrite(strFileName);     //打开继续写入,并从尾部开始,用于断点续传
                sPosstion = FStream.Length;
                FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针
            }
            else
            {
                FStream   = new FileStream(strFileName, FileMode.Create);
                sPosstion = 0;
            }
            //打开网络连接
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                //if (CompletedLength > 0)
                //    myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件..
                //向服务器请求,获得服务器的回应数据流
                HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse();
                long            FileLength  = webResponse.ContentLength;//文件大小
                percent = FileLength / 100;
                Stream myStream  = webResponse.GetResponseStream();
                byte[] btContent = new byte[1024];
                //if (count <= 0) count += sPosstion;//

                //开始写入
                int count = 0;
                while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完
                {
                    FStream.Write(btContent, 0, count);                 //知道有多少个数字节后再写入
                    CompletedLength += count;
                    if (!(CompletedLength / percent).ToString().Equals(temp))
                    {
                        temp       = (CompletedLength / percent).ToString();
                        progStatus = temp;
                    }
                }
                myStream.Close();
            }
            finally
            {
                FStream.Close();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="strFileName">保存文件的全路径</param>
        /// <param name="md5">md5</param>
        /// <param name="http">网络地址</param>
        /// <param name="callback">回调</param>
        /// <returns></returns>
        public static bool DownloadFile(string strFileName, string md5, string http, Action <int> callback, Action completed = null)
        {
            var        flag    = false;
            FileStream FStream = null;

            try
            {
                long SPosition = 0;
                if (File.Exists(strFileName))
                {
                    if (md5 == GetFileMd5(strFileName))//如果文件相同
                    {
                        return(true);
                    }
                    FStream   = File.OpenWrite(strFileName);
                    SPosition = FStream.Length;//断点续传
                    FStream.Seek(SPosition, SeekOrigin.Current);
                }
                else
                {
                    FStream   = new FileStream(strFileName, FileMode.Create);
                    SPosition = 0;
                }

                var myRequest = (HttpWebRequest)WebRequest.Create(http);
                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);
                }
                var myStream  = myRequest.GetResponse().GetResponseStream();
                var btContent = new byte[512];
                if (myStream != null)
                {
                    var intSize = myStream.Read(btContent, 0, 512);
                    while (intSize > 0)
                    {
                        callback?.Invoke(intSize);
                        FStream.Write(btContent, 0, intSize);
                        intSize = myStream.Read(btContent, 0, 512);
                    }
                    FStream.Close();
                    myStream.Close();
                }
                flag = true;
            }
            catch (Exception ex)
            {
                FStream?.Close();
                flag = false;
                File.Delete(strFileName);
            }
            if (flag)
            {
                completed?.Invoke();
            }
            return(flag);
        }
Ejemplo n.º 5
0
        ///
        /// 下载文件方法
        ///
        /// 文件保存路径和文件名
        /// 返回服务器文件名
        ///
        bool DownloadFile(string DownUrl, string SaveFileName)
        {
            bool flag = false;
            //打开上次下载的文件
            long SPosition = 0;
            //实例化流对象
            FileStream FStream;

            //判断要下载的文件夹是否存在
            if (File.Exists(SaveFileName))
            {
                //打开要下载的文件
                FStream = File.OpenWrite(SaveFileName);
                //获取已经下载的长度
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);
            }
            else
            {
                //文件不保存创建一个文件
                FStream   = new FileStream(SaveFileName, FileMode.Create);
                SPosition = 0;
            }
            try
            {
                //打开网络连接
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(DownUrl);
                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);             //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                Stream 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);
                }
                //关闭流
                FStream.Close();
                myStream.Close();
                flag = true;        //返回true下载成功
            }
            catch (Exception)
            {
                FStream.Close();
                flag = false;       //返回false下载失败
            }
            return(flag);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 下载单个文件
        /// </summary>
        /// <param name="savePath">保存路径</param>
        /// <param name="url">下载地址</param>
        /// <param name="isContinue">是否断点续传</param>
        /// <param name="isRetryAllways">失败后是否一直重试</param>
        /// <returns>小于0失败</returns>
        public int DownloadFile(string savePath, string url, bool isContinue = true)
        {
            UpdateLog.INFO_LOG(_TAG + "DownloadFile(string strFileName, string url, bool isContinue = true) : " +
                               savePath + "," +
                               url + "," +
                               isContinue);

            int  downloadRet = CodeDefine.RET_FAIL;
            long SPosition   = 0;

            //如果本地有下载文件,则判断是否断点续传
            FileStream FStream = null;

            try
            {
                ExistFileSize = 0;
                if (File.Exists(savePath) && isContinue)
                {
                    //定向文件写入位置
                    FStream   = File.Open(savePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                    SPosition = FStream.Length;
                    FStream.Seek(SPosition, SeekOrigin.Current);
                    //已存在文件大小
                    ExistFileSize = (int)SPosition;
                }
                else
                {
                    FStream   = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                    SPosition = 0;
                }

                //开始下载
                downloadRet = HttpDownload(url, FStream, SPosition);
                if (FStream.CanWrite)
                {
                    FStream.Close();
                }
                Thread.Sleep(_SLEEP_TIME);
            }
            catch (System.Exception ex)
            {
                UpdateLog.ERROR_LOG(ex.Message);
                UpdateLog.EXCEPTION_LOG(ex);
            }
            finally
            {
                if (FStream != null && FStream.CanWrite)
                {
                    FStream.Close();
                }
            }

            return(downloadRet);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 以断点续传方式下载文件
        /// </summary>
        /// <param name="strFileName">下载文件的保存路径</param>
        /// <param name="strUrl">文件下载地址</param>
        public static void DownloadFile(string strUrl)
        {
            var   strFileName = AppDomain.CurrentDomain.BaseDirectory + @"DownloadGeam\" + strUrl.Substring(strUrl.LastIndexOf("/") + 1);
            float percent     = 0;
            //打开上次下载的文件或新建文件
            long       SPosition = 0;
            FileStream FStream;

            if (File.Exists(strFileName))
            {
                FStream   = File.OpenWrite(strFileName);
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);//移动文件流中的当前指针
            }
            else
            {
                FStream   = new FileStream(strFileName, FileMode.Create);
                SPosition = 0;
            }
            //打开网络连接
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);//设置Range值
                }
                long totalBytes = myRequest.ContentLength;

                //向服务器请求,获得服务器的回应数据流
                Stream myStream            = myRequest.GetResponse().GetResponseStream();
                byte[] btContent           = new byte[1024];
                int    intSize             = 0;
                long   totalDownloadedByte = 0;
                intSize = myStream.Read(btContent, 0, 1024);
                while (intSize > 0)
                {
                    totalDownloadedByte = intSize + totalDownloadedByte;
                    FStream.Write(btContent, 0, intSize);
                    intSize = myStream.Read(btContent, 0, 1024);

                    percent = totalDownloadedByte / (float)totalBytes * 100;
                }
                FStream.Close();
                myStream.Close();
            }
            catch (Exception ex)
            {
                FStream.Close();
            }
        }
Ejemplo n.º 8
0
        void DownFile_FormClosed(object sender, FormClosedEventArgs e)
        {
            BufferFormatV2 buff = new BufferFormatV2((int)FileCmd.DownClose);

            buff.AddItem(down.DownKey);
            user.SendData(buff.Finish());

            if (FStream != null)
            {
                FStream.Close();
            }

            user.DownDataOn = null;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 以断点续传方式下载文件
        /// </summary>
        /// <param name="strFileName">下载文件的保存路径</param>
        /// <param name="strUrl">文件下载地址</param>
        public void DownloadFile(string strFileName, string strUrl)
        {
            //打开上次下载的文件或新建文件
            int CompletedLength = 0;//记录已完成的大小

            FileStream FStream;

            if (File.Exists(strFileName))
            {
                FStream   = File.OpenWrite(strFileName);
                sPosstion = FStream.Length;
                FStream.Seek(sPosstion, SeekOrigin.Current);//移动文件流中的当前指针
            }
            else
            {
                FStream   = new FileStream(strFileName, FileMode.Create);
                sPosstion = 0;
            }
            //打开网络连接
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                if (CompletedLength > 0)
                {
                    myRequest.AddRange((int)CompletedLength);//设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse();
                FileLength = webResponse.ContentLength;//文件大小
                Stream myStream  = webResponse.GetResponseStream();
                byte[] btContent = new byte[1024];
                if (count <= 0)
                {
                    count += sPosstion;
                }

                while ((CompletedLength = myStream.Read(btContent, 0, 1024)) > 0)
                {
                    FStream.Write(btContent, 0, CompletedLength);
                    count += CompletedLength;
                }
                FStream.Close();
                myStream.Close();
                MessageBox.Show("文件下载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                FStream.Close();
            }
        }
Ejemplo n.º 10
0
        void DownFile_FormClosed(object sender, FormClosedEventArgs e)
        {
            BufferFormatV2 buff = new BufferFormatV2((int)PackType.DownClose);

            buff.AddItem(down.DownKey);
            SocketManager.Send(buff.Finish());


            if (FStream != null)
            {
                FStream.Close();
            }

            SocketManager.BinaryInput -= new ZYSocket.ClientB.ClientBinaryInputHandler(SocketManager_BinaryInput);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 从指定站点下载文件,并保存在指定位置,//下载,迁入ZipClass
        /// </summary>
        /// <param name="strUrl">目标Url</param>
        /// <param name="strFileName">本地物理路径</param>
        /// <param name="flag">appliatoin[flag]用于存进度</param>
        /// <param name="ct">当前上下文HttpContext</param>
        /// <param name="begin">从指定位置开始下载(未实现)</param>
        public void DownloadFile(string strUrl, string strFileName, string flag = "", HttpContext ct = null, int begin = 0)
        {
            //已完成的,1%长度
            long percent   = 0;
            long sPosstion = 0; //磁盘现盘文件的长度
                                //long count = 0;// count += sPosstion,从指定位置开始写入字节
            FileStream FStream;

            if (File.Exists(strFileName))                    //如果文件存在
            {
                FStream   = File.OpenWrite(strFileName);     //打开继续写入,并从尾部开始,用于断点续传
                sPosstion = FStream.Length;
                FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针
            }
            else
            {
                FStream   = new FileStream(strFileName, FileMode.Create);
                sPosstion = 0;
            }
            //打开网络连接
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                //if (CompletedLength > 0)//断点续传
                //    myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件..
                //向服务器请求,获得服务器的回应数据流
                HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse();
                long            FileLength  = webResponse.ContentLength;//文件大小
                percent = FileLength / 100;
                Stream myStream  = webResponse.GetResponseStream();
                byte[] btContent = new byte[myStream.Length];
                myStream.Read(btContent, 0, btContent.Length);
                SafeSC.SaveFile(Path.GetDirectoryName(strFileName) + "\\", Path.GetFileName(strFileName), btContent);
                //while ((count = myStream.Read(btContent, 0, 1024)) > 0)//返回读了多少字节,为0表示全部读完
                //{
                //    FStream.Write(btContent, 0, count);//知道有多少个数字节后再写入
                //    CompletedLength += count;
                //    if(ct!=null&&!string.IsNullOrEmpty(flag))
                //         ct.Application[flag] = CompletedLength;
                //}
                myStream.Close();
            }
            finally
            {
                FStream.Close();
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 以断点续传方式下载文件
        /// </summary>
        /// <param name="strFileName">下载文件的保存路径</param>
        /// <param name="strUrl">文件下载地址</param>
        public void DownloadFile(string strFileName, string strUrl)
        {
            //打开上次下载的文件或新建文件
            long       SPosition = 0;
            FileStream FStream;

            if (File.Exists(strFileName))
            {
                FStream   = File.OpenWrite(strFileName);
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);//移动文件流中的当前指针
            }
            else
            {
                FStream   = new FileStream(strFileName, FileMode.Create);
                SPosition = 0;
            }
            //打开网络连接
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);//设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                Stream 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);
                }
                FStream.Close();
                myStream.Close();
                MessageBox.Show("文件下载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                FStream.Close();
            }
        }
Ejemplo n.º 13
0
        protected override void Dispose(bool ADisposing)
        {
            if (FProcess != null)
            {
                if (FProcess.StreamManager != null)
                {
                    FProcess.StreamManager.Close(FStreamID);
                }
                FProcess = null;
            }

            if (FStream != null)
            {
                FStream.Close();
                FStream = null;
            }

            base.Dispose(ADisposing);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 从指定服务器上下载文件,支持断点续传
        /// </summary>
        /// <param name="url">目标Url</param>
        /// <param name="vpath">本地虚拟路径</param>
        /// <param name="begin">开始位置,默认为0</param>
        public void DownFile(string url, string vpath, int begin = 0)
        {
            vpath = SafeSC.PathDeal(vpath);
            if (SafeSC.FileNameCheck(vpath))
            {
                throw new Exception("不支持下载[" + Path.GetFileName(vpath) + "]文件");
            }
            string ppath = function.VToP(vpath);

            //已完成的,1%长度
            int  CompletedLength = 0;
            long percent = 0; string temp = "0";
            long sPosstion = 0;//磁盘现盘文件的长度
            //long count = 0;// count += sPosstion,从指定位置开始写入字节
            FileStream FStream;

            if (File.Exists(ppath))
            {
                FStream   = File.OpenWrite(ppath);           //打开继续写入,并从尾部开始,用于断点续传(如果不需要,则应该删除其)
                sPosstion = FStream.Length;
                FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针
            }
            else
            {
                string dir = Path.GetDirectoryName(ppath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                FStream   = new FileStream(ppath, FileMode.Create);
                sPosstion = 0;
            }
            //打开网络连接
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                //if (CompletedLength > 0)
                //    myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件..
                //向服务器请求,获得服务器的回应数据流
                HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse();
                long            FileLength  = webResponse.ContentLength;//文件大小
                percent = FileLength / 100;
                Stream myStream  = webResponse.GetResponseStream();
                byte[] btContent = new byte[1024];
                //if (count <= 0) count += sPosstion;//
                //开始写入
                int count = 0;
                while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完
                {
                    FStream.Write(btContent, 0, count);                 //知道有多少个数字节后再写入
                    CompletedLength += count;
                    if (!(CompletedLength / percent).ToString().Equals(temp))
                    {
                        temp = (CompletedLength / percent).ToString();
                        //progStatus = temp;
                    }
                }
                myStream.Close();
            }
            finally
            {
                FStream.Close();
            }
        }
Ejemplo n.º 15
0
        ///
        /// 下载文件方法 支持断点续传
        ///
        /// 文件保存路径和文件名
        /// 服务器文件名
        public bool DownloadFile(CookieContainer cookie, string strFileName, string fileUrl)
        {
            bool flag = false;
            //打开上次下载的文件
            long SPosition = 0;
            //实例化流对象
            FileStream FStream;

            //判断要下载的文件是否存在
            if (File.Exists(strFileName))
            {
                //打开要下载的文件
                FStream = File.OpenWrite(strFileName);
                //获取已经下载的长度
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);
                //File.Delete(strFileName);
            }
            else
            {
                //文件不保存创建一个文件
                FStream   = new FileStream(strFileName, FileMode.Create);
                SPosition = 0;
            }

            HttpWebRequest  myRequest  = null;
            HttpWebResponse myResponse = null;

            try
            {
                //打开网络连接
                myRequest = (HttpWebRequest)HttpWebRequest.Create(GetUrl(fileUrl));
                //myRequest.AllowAutoRedirect = true;
                myRequest.CookieContainer = cookie;
                //OnDownloadProgress(SPosition, total ,strFileName );

                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);             //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                myResponse = myRequest.GetResponse() as HttpWebResponse;
                //myResponse.Headers.Add("content-reanghe", SPosition.ToString() );
                long   total    = myResponse.ContentLength;
                Stream myStream = myResponse.GetResponseStream();

                //定义一个字节数据
                int    bufflength = 1024;
                byte[] btContent  = new byte[bufflength];
                int    intSize    = 0;
                intSize = myStream.Read(btContent, 0, bufflength);

                SPosition += intSize;
                OnDownloadProgress(SPosition, total, strFileName);

                while (intSize > 0)
                {
                    FStream.Write(btContent, 0, intSize);
                    intSize = myStream.Read(btContent, 0, bufflength);

                    SPosition += intSize;
                    OnDownloadProgress(SPosition, total, strFileName);
                }

                myStream.Close();
                flag = true;        //返回true下载成功

                OnDownloadCompleted(null, true, null);
            }
            catch (Exception ex)
            {
                flag = false;       //返回false下载失败
                OnDownloadCompleted(ex, false, strFileName);
            }
            finally
            {
                //关闭流
                if (FStream != null)
                {
                    FStream.Close();
                    FStream = null;
                }
                if (myResponse != null)
                {
                    myResponse.Close();
                    myResponse = null;
                }
                if (myRequest != null)
                {
                    myRequest.Abort();
                    myRequest = null;
                }
            }
            return(flag);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// 从台账服务器拿db文件http的方式
        /// </summary>
        /// <param name="infoString"></param>
        /// <returns></returns>

        public static bool TZDown(string strFileName, string TZdz, string ftplj)
        {
            //将文件名改一下
            strFileName = strFileName + ".wait";

            bool flag = false;
            //打开上次下载的文件
            long SPosition = 0;
            //实例化流对象
            FileStream FStream;

            //判断要下载的文件夹是否存在
            if (File.Exists(strFileName))
            {
                //打开要下载的文件
                FStream = File.OpenWrite(strFileName);
                //获取已经下载的长度
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);
            }
            else
            {
                //文件不保存创建一个文件
                FStream   = new FileStream(strFileName, FileMode.Create);
                SPosition = 0;
            }
            try
            {
                //打开网络连接
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(TZdz);
                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);             //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                Stream myStream = myRequest.GetResponse().GetResponseStream();
                //定义一个字节数据
                byte[] btContent = new byte[1024];
                int    intSize   = 0;
                intSize = myStream.Read(btContent, 0, 1024);
                while (intSize > 0)
                {
                    FStream.Write(btContent, 0, intSize);
                    intSize = myStream.Read(btContent, 0, 1024);
                }
                FStream.Flush();
                //关闭流
                FStream.Close();
                myStream.Close();

                //将文件名改回来
                string tempName = strFileName;
                strFileName = strFileName.Substring(0, strFileName.Length - 5);
                File.Move(tempName, strFileName);
                flag = true;        //返回true下载成功
            }
            catch (Exception ex)
            {
                PublicMethod.write_state(ftplj + "\\", -1.01);
                CYZFramework.Log.CYZLog.writeLog(ex.ToString());

                FStream.Close();
                flag = false;       //返回false下载失败
            }
            return(flag);
        }
Ejemplo n.º 17
0
        //通过HTTP下载文件
        public static bool DownloadFile(string url, string localFileName)
        {
            bool flag = false;
            //打开上次下载的文件
            long SPosition = 0;
            //实例化流对象
            FileStream FStream;

            //判断要下载的文件夹是否存在
            if (File.Exists(localFileName))
            {
                //打开要下载的文件
                FStream = File.OpenWrite(localFileName);
                //获取已经下载的长度
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);
            }
            else
            {
                //文件不保存创建一个文件
                FStream   = new FileStream(localFileName, FileMode.Create);
                SPosition = 0;
            }
            try
            {
                //打开网络连接
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);

                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    myRequest.ProtocolVersion = HttpVersion.Version11;
                    myRequest.UserAgent       = DefaultUserAgent;
                }

                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);             //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                WebResponse response = (HttpWebResponse)myRequest.GetResponse();
                Stream      myStream = response.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);
                }
                //关闭流
                FStream.Close();
                myStream.Close();
                flag = true;        //返回true下载成功
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                FStream.Close();
                flag = false;       //返回false下载失败
            }
            return(flag);
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
        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);
        }
Ejemplo n.º 20
0
        //Html必须编码后再传输,html默认的是将其UrlEncode后传递,服务端再编辑,直接传是不能的
        //public M_Http_Result UploadParam(string url, Dictionary<string, object> postParameters)
        //{
        //    HttpMultipartFormRequest req = new HttpMultipartFormRequest();
        //    //req.config = config;
        //    M_Http_Result result = new M_Http_Result();
        //    result = req.AsyncHttpRequest(url, postParameters, null);
        //    return result;
        //}
        #endregion


        #region 文件下载
        /// <summary>
        /// 从指定服务器上下载文件,支持断点续传
        /// </summary>
        /// <param name="url">目标Url</param>
        /// <param name="vpath">本地虚拟路径</param>
        /// <param name="begin">开始位置,默认为0</param>
        public static void DownFile(string url, string vpath, int begin = 0)
        {
            //尝试除以0,原因:下载的文字过小,占用字节为0
            vpath = SafeSC.PathDeal(vpath);
            if (SafeSC.FileNameCheck(vpath))
            {
                throw new Exception("不支持下载[" + Path.GetFileName(vpath) + "]文件");
            }
            string ppath = function.VToP(vpath);

            //long percent = 0;
            long sPosstion = 0;//磁盘现盘文件的长度
            //long count = 0;// count += sPosstion,从指定位置开始写入字节
            FileStream FStream;

            if (File.Exists(ppath))
            {
                FStream   = File.OpenWrite(ppath);           //打开继续写入,并从尾部开始,用于断点续传(如果不需要,则应该删除其)
                sPosstion = FStream.Length;
                FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针
            }
            else
            {
                string dir = Path.GetDirectoryName(ppath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                try { FStream = new FileStream(ppath, FileMode.Create); }
                catch (Exception ex) { throw new Exception(ex.Message + "||" + ppath); }
                sPosstion = 0;
            }
            //打开网络连接
            Stream myStream = null;

            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                //if (CompletedLength > 0)
                //    myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件..
                //向服务器请求,获得服务器的回应数据流
                HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse();
                //long FileLength = webResponse.ContentLength;//文件大小
                //percent = FileLength / 100;
                myStream = webResponse.GetResponseStream();
                byte[] btContent = new byte[1024];
                //开始写入
                int count = 0;
                while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完
                {
                    FStream.Write(btContent, 0, count);                 //知道有多少个数字节后再写入
                }
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("基础连接已经关闭"))
                {
                    throw new Exception("DownFile:" + ex.Message + "|" + url);
                }
            }
            finally
            {
                if (myStream != null)
                {
                    myStream.Close();
                }
                if (FStream != null)
                {
                    FStream.Close();
                }
            }
        }
Ejemplo n.º 21
0
        public static bool DownLoad_OffLine(string fileId, string UpLoadFileName, string DownloadFileName, string saveDir,
                                            int upSize,
                                            DataRow drmission)
        {
            bool flag = false;

            string uri = SysParams.FileServer + SysParams.FileServer_tmpFile + "/" + fileId + "/" + UpLoadFileName;
            //截取文件名
            string fileName = UpLoadFileName;
            //构造文件完全限定名,准备将网络流下载为本地文件
            string fileFullName = saveDir + DownloadFileName + SysParams.tmpDownLoadName;

            //打开上次下载的文件
            long SPosition = 0;
            //实例化流对象
            FileStream FStream;

            //判断要下载的文件夹是否存在
            if (File.Exists(fileFullName))
            {
                //打开要下载的文件
                FStream = File.OpenWrite(fileFullName);
                //获取已经下载的长度
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);
            }
            else
            {
                //文件不保存创建一个文件
                FStream   = new FileStream(fileFullName, FileMode.Create);
                SPosition = 0;
            }
            try
            {
                //打开网络连接
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
                if (SPosition > 0)
                {
                    myRequest.AddRange((int)SPosition);             //设置Range值
                }
                //向服务器请求,获得服务器的回应数据流
                Stream myStream = myRequest.GetResponse().GetResponseStream();
                //定义一个字节数据
                byte[] btContent = new byte[upSize];
                int    intSize   = 0;
                intSize = myStream.Read(btContent, 0, upSize);
                while (intSize > 0)
                {
                    FStream.Write(btContent, 0, intSize);
                    intSize = myStream.Read(btContent, 0, upSize);
                    lock (drmission.Table)//更新已上传量
                    {
                        drmission["UploadSize"] = FStream.Length;
                    }
                }
                //关闭流
                FStream.Close();
                myStream.Close();
                flag = true;        //返回true下载成功
            }
            catch (Exception)
            {
                FStream.Close();
                flag = false;       //返回false下载失败
            }
            return(flag);
        }