zoukankan      html  css  js  c++  java
  • 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)
                {
                    //返回错误消息
                }
            }
  • 相关阅读:
    android获得屏幕高度和宽度
    Android中RelativeLayout各个属性的含义
    Android JNI知识简介
    Android结束进程方法
    android开发中的图片资源问题
    android framework下public.xml中添加资源问题
    I'm back
    test,exec,match,replace方法区别 正则
    fiddler抓包工具 https抓取 ios手机端抓取
    前端优化方案
  • 原文地址:https://www.cnblogs.com/JLZT1223/p/6927279.html
Copyright © 2011-2022 走看看