Exemplo n.º 1
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);
        }