zoukankan      html  css  js  c++  java
  • C#分块拷贝大文件

      //定义源文件和目标文件,绝对路径
       public static string source = @"E:C#C#编程语言详解.pdf";
        //2014-6-10 Training
                //拷贝大文件,分块拷贝
                AddBigFile(source);
        /// <summary>
            /// 拷贝大文件
            /// </summary>
            /// <param name="source">原绝对路径</param>
            private static void AddBigFile(string source)
            {
                int i = 0;
                using (FileStream fsRead = new FileStream(source, FileMode.Open))
                {
                    byte[] byts = new byte[1024 * 1024 * 10];
                    while (true)
                    {
                        int r = fsRead.Read(byts, 0, byts.Length);
    
                        if (r <= 0)
                        {
                            Console.WriteLine("----End----");
                            break;
                        }
                        DataBaseController.AddFiles(source, i, byts);
                        Console.WriteLine("FileName:" + source + "" + i + "" + "大小:" + byts.Length);
                        i++;
                    }
                }
            }
    
    DataBaseController.AddFiles函数是拿到二进制数据,插入数据库操作:
    
     public static class DataBaseController
        {
           public static readonly string connstr = "Data Source=.;Initial Catalog=AddFile;Persist Security Info=True;User ID=sa;Password=sa";
           public static string Tosource = @"E:C#Copy123C#编程语言详解.pdf";
    
           public static void AddFiles(string FileName,int Code,byte[] Data)
           {
               string sql = @"insert into FileBlock(FileName, Code, Data) values(@FileName, @Code, @Data)";
               DateTime time = DateTime.Now;
               SqlParameter[] pars = {
                                    new SqlParameter("@FileName",FileName),
                                    new SqlParameter("@Code",Code),
                                    new SqlParameter("@Data",Data)
                                   
                                    };
               int count = SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, sql, pars);
           }
    
           public static void GetFilesByName(string FileName)
           {
               string sql = @"select * from FileBlock where FileName = @FileName";
               DateTime time = DateTime.Now;
               SqlParameter[] pars = {
                                    new SqlParameter("@FileName",FileName),                    
                                    };
               List<FileBlock> list = new List<FileBlock>();
               using (SqlDataReader reader = SqlHelper.ExecuteReader(connstr, CommandType.Text, sql, pars))
               {
                   list = Application.Data2Objects<FileBlock>(reader);
               }
    
               //循环查出文件的多个块,拼接写在一个文件中
               FileStream fs = new FileStream(Tosource,FileMode.Create);
               foreach (var item in list)
               {
                   fs.Write(item.Data,0,item.Data.Length);
               }
    
               //byte[] data = new byte[fs.Length];
               //fs.Read(data,0,fs.Length);
               //fs.Seek(0,SeekOrigin.Begin);
              // return data;
           }
        }
    
       public class FileBlock
       {
           public int ID { get; set; }
           public string  FileName { get; set; }
           public int Code { get; set; }
           public byte[] Data { get; set; }
       }
  • 相关阅读:
    ffmpeg+nginx+video实现rtsp流转hls流,通过H5查看监控视频
    视频支持拖动进度条播放的实现(基于nginx)
    nginx 点播mp4方法
    NGINX 添加MP4、FLV视频支持模块
    html5播放mp4视频代码
    ThreadPoolExecutor使用错误导致死锁
    Spring-Cloud-Gateway
    从0开始构建你的api网关--Spring Cloud Gateway网关实战及原理解析
    使用Consul做服务发现的若干姿势
    Consul 的安装与基本使用
  • 原文地址:https://www.cnblogs.com/kennyliu/p/3839908.html
Copyright © 2011-2022 走看看