下载远程服务器文件


        /// <summary>
        /// 下载远程服务器文件
        /// </summary>
        /// <param name="url">远程地址</param>
        /// <param name="path">下载到的本地路径</param>
        /// <param name="filename">文件名称</param>
        public void DownloadUrlFile(string url, string path, string filename)
        {
            string pathUrl = url;
            System.Net.HttpWebRequest request = null;
            System.Net.HttpWebResponse response = null;
            //请求网络路径地址
            request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(pathUrl);
            request.Timeout = 5000; // 超时时间
            //获得请求结果
            response = (System.Net.HttpWebResponse)request.GetResponse();
            //文件下载地址
            // 如果不存在就创建file文件夹
            if (!Directory.Exists(path))
            {
                if (path != null) Directory.CreateDirectory(path);
            }
            path = path + "/" + filename;
            Stream stream = response.GetResponseStream();
            //先创建文件
            Stream sos = new System.IO.FileStream(path, System.IO.FileMode.Create);
            byte[] img = new byte[1024];
            int total = stream.Read(img, 0, img.Length);
            while (total > 0)
            {
                //之后再输出内容
                sos.Write(img, 0, total);
                total = stream.Read(img, 0, img.Length);
            }
            stream.Close();
            stream.Dispose();
            sos.Close();
            sos.Dispose();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值