• c#线程中下载文件到本地


    额,太懒了 直接上示例代码。。。

            /// <summary>
            /// 下载文件到本地   2017-05-31
            /// </summary>
            /// <param name="DownloadPath">本地下载目录</param>
            /// <param name="FullFilePath">下载地址</param>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            private void DownLoadSoft(string DownloadPath, string FullFilePath, string FileName)
            {
                ParameterizedThreadStart s = new ParameterizedThreadStart(DownLoadSoftAu);
                Thread thread = new Thread(s);
                thread.IsBackground = true;
                thread.Start(new object[] { DownloadPath, FullFilePath, FileName });
                thread.Join();
            }
            private void DownLoadSoftAu(object obj)
            {
            
                string DownloadPath = ((object[])obj)[0].ToString().Trim();
                string FullFilePath =((object[])obj)[1].ToString().Trim();
                string FileName = ((object[])obj)[2].ToString().Trim();
                try
                {
                    if (!Directory.Exists(DownloadPath))
                    {
                        Directory.CreateDirectory(DownloadPath);
                    }
                    using (FileStream fs = new FileStream(DownloadPath + FileName, 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)
                        {
                            fs.Write(bufferBytes, 0, bytesRead);
                        }         
                        //关闭写入
                        fs.Flush();
                        fs.Close();
                    }
    
                }
                catch (Exception exp)
                {
                    //返回错误消息
                }
            }
  • 相关阅读:
    POJ 1320 Street Numbers 解佩尔方程
    数学分支(转)
    深入理解Java类加载器(1):Java类加载原理解析
    Java类加载器的工作原理
    深入理解Java:类加载机制及反射
    类加载机制:全盘负责和双亲委托
    java底层学习
    代码面试最常用的10大算法
    程序员面试金典算法题
    了解ASCII、gb系列、Unicode、UTF-8的区别
  • 原文地址:https://www.cnblogs.com/JLZT1223/p/6927279.html
Copyright © 2020-2023  润新知