C# 下载远程文件

            #region test
            string fullFilePath = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
            DownloadFile(fullFilePath);

            DownloadFile2(fullFilePath);
            #endregion
        #region 下载1
        public static bool DownloadFile(string fullFilePath)
        {
            try
            {
                string basePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "temp");
                if (!Directory.Exists(basePath))
                    Directory.CreateDirectory(basePath);

                string extension = Path.GetExtension(fullFilePath);
                Random random = new Random();
                string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + Guid.NewGuid().ToString().Replace("-", "").Substring(9, 16) + random.Next(100000, 999999) + extension;
                string saveFilePath = Path.Combine(basePath, fileName);
                using (WebClient webClient = new WebClient())
                {
                    webClient.DownloadFile(fullFilePath, saveFilePath);
                }
                if (File.Exists(saveFilePath))
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 下载2
        public static bool DownloadFile2(string fullFilePath)
        {
            bool flag = false;
            try
            {
                string basePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "temp");
                if (!Directory.Exists(basePath))
                    Directory.CreateDirectory(basePath);

                string extension = Path.GetExtension(fullFilePath);
                Random random = new Random();
                string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + Guid.NewGuid().ToString().Replace("-", "").Substring(9, 16) + random.Next(100000, 999999) + extension;
                string saveFilePath = Path.Combine(basePath, fileName);
                using (FileStream fileStream = new FileStream(saveFilePath, FileMode.Create, FileAccess.Write))
                {
                    //创建请求
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullFilePath);
                    //接收响应
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    //输出流
                    Stream responseStream = response.GetResponseStream();
                    byte[] bufferBytes = new byte[10000];//缓冲字节数组
                    int bytesRead = -1;
                    while ((bytesRead = responseStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0)
                    {
                        fileStream.Write(bufferBytes, 0, bytesRead);
                    }
                    if (fileStream.Length > 0)
                    {
                        flag = true;
                    }
                    //关闭写入
                    fileStream.Flush();
                    fileStream.Close();
                }
                return flag;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值