Ejemplo n.º 1
0
 /// <summary>
 /// 获取文件片段信息
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="system"></param>
 /// <returns></returns>
 public FileSliceInfo GetFileSliceInfo(string fileName, SeatManageSubsystem system)
 {
     try
     {
         //fileName = fileName.Substring(fileName.IndexOf("\\"));
         string        filepath = uploadFolder + string.Format(@"{0}\", system.ToString()) + fileName;
         FileSliceInfo file     = new FileSliceInfo();
         file.Name   = fileName;
         file.Length = 0;
         file.Offset = 0;
         file.Data   = null;
         if (System.IO.File.Exists(filepath + ".tz"))
         {
             System.IO.FileInfo fi = new System.IO.FileInfo(filepath + ".tz");
             file.Length = fi.Length;
             file.Offset = fi.Length;//返回追加数据后的文件位置
             fi          = null;
             return(file);
         }
         else
         {
             return(null);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fullPath">文件完整路径</param>
        /// <param name="relativePath">相对路径</param>
        /// <param name="system">系统类型</param>
        /// <returns></returns>
        public bool UpdateFile(string fullPath, string relativePath, SeatManageSubsystem system)
        {
            //上传出错重试,每次重试50次,每次上传成功,初始化重试次数  2013-9-4 王随
            for (int i = 0; i < 50; i++)
            {
                try
                {
                    string     fileName = relativePath; // fullPath.Substring(fullPath.LastIndexOf(@"\") + 1);
                    int        maxSiz   = 1024 * 1000;  //设置每次传100k
                    FileStream stream   = System.IO.File.OpenRead(fullPath);
                    //读取本地文件
                    FileSliceInfo file = FileTransportBll.GetFileSliceInfo(fileName, system); //更加文件名,查询服务中是否存在该文件
                    if (file == null)                                                         //表示文件不存在
                    {
                        file        = new FileSliceInfo();
                        file.Offset = 0; //设置文件从开始位置进行数据传递
                    }
                    file.Length = stream.Length;
                    if (file.Length == file.Offset)
                    //如果文件的长度等于文件的偏移量,说明文件已经上传完成 ,重新上传
                    {
                        file.Offset = 0; //设置文件从开始位置进行数据传递
                    }
                    file.Name = fileName;
                    while (file.Length != file.Offset)                                                                        //循环的读取文件,上传,直到文件的长度等于文件的偏移量
                    {
                        file.Data       = new byte[file.Length - file.Offset <= maxSiz ? file.Length - file.Offset : maxSiz]; //设置传递的数据的大小
                        stream.Position = file.Offset;                                                                        //设置本地文件数据的读取位置
                        stream.Read(file.Data, 0, file.Data.Length);                                                          //把数据写入到file.Data中
                        file = FileTransportBll.FileUpLoad(file, system);                                                     //上传
                        i    = 0;                                                                                             //初始化重试次数
                        if (HandleProgress != null)
                        {
                            int progress = (int)((double)file.Offset / (double)file.Length * 100);
                            HandleProgress(progress);
                        }
                    }

                    stream.Close();
                    stream.Dispose();
                    stream = null;

                    return(true);
                }
                catch
                {
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 获取文件信息
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="system"></param>
 /// <returns></returns>
 public FileSliceInfo GetFileInfo(string fileName, SeatManageSubsystem system)
 {
     try
     {
         string filepath = "";
         if (fileName.IndexOf(@"\") == 0)//如果\第一次出现的位置为1,则不用加斜杠
         {
             filepath = string.Format("{0}{1}{2}", uploadFolder, system.ToString(), fileName);
         }
         else
         {
             filepath = string.Format(@"{0}{1}\{2}", uploadFolder, system.ToString(), fileName);
         }
         FileSliceInfo file = new FileSliceInfo();
         file.Name   = fileName;
         file.Length = 0;
         file.Offset = 0;
         file.Data   = null;
         if (System.IO.File.Exists(filepath))
         {
             //文件已经存在
             System.IO.FileInfo fi = new System.IO.FileInfo(filepath);
             file.Length = fi.Length;
             file.Offset = fi.Length;//返回追加数据后的文件位置
             fi          = null;
             return(file);
         }
         else
         {
             return(null);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 文件下载断点续传
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="Offset"></param>
        /// <returns></returns>
        public static FileSliceInfo FileDownLoad(FileSliceInfo file, SeatManageSubsystem system)
        {
            IFileTransportService fileTransport = WcfAccessProxy.ServiceProxy.CreateChannelFileTransportService();
            bool isError = false;

            try
            {
                file.Data = fileTransport.FileDownLoad(file.Name, file.Offset, system);
                return(file);
            }
            catch (Exception ex)
            {
                isError = true;
                WriteLog.Write(string.Format("文件下载出错,异常信息:{0}", ex.Message));
                throw ex;
            }
            finally
            {
                ICommunicationObject ICommObjectService = fileTransport as ICommunicationObject;
                try
                {
                    if (ICommObjectService.State == CommunicationState.Faulted)
                    {
                        ICommObjectService.Abort();
                    }
                    else
                    {
                        ICommObjectService.Close();
                    }
                }
                catch
                {
                    ICommObjectService.Abort();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 下载文件
        /// 每次下载遇到错误重试50次
        /// </summary>
        /// <param name="filePath">文件的完整</param>
        /// <param name="relativePath">文件的相对路径</param>
        /// <param name="system">系统名称</param>
        /// <returns></returns>
        public string FileDownLoad(string filePath, string relativePath, SeatManageSubsystem system)
        {
            //添加下载错误自动断点下载,每次出错重试50次。 2013-9-4 作者:王随
            for (int i = 0; i < 50; i++)
            {
                //获取文件的路径,已经保存的文件名
                FileStream   fs     = null;
                BinaryWriter writer = null;
                try
                {
                    string        fileName = relativePath;// filePath.Substring(filePath.LastIndexOf(@"\") + 1);
                    string        dirPath  = filePath.Substring(0, filePath.LastIndexOf(@"\") + 1);
                    FileSliceInfo file2    = FileTransportBll.GetFileInfo(fileName, system);
                    if (file2 == null)
                    {
                        if (DownloadError != null)
                        {
                            DownloadError(string.Format("文件{0}下载失败。", fileName));
                        }
                        return(string.Format("文件{0}下载失败。", fileName));
                    }
                    file2.Name   = fileName;
                    file2.Offset = 0;

                    //如果文件目录不存在,创建文件所存放的目录.
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }


                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                    fs = new FileStream(filePath + ".tz", FileMode.OpenOrCreate);

                    file2.Offset = fs.Length;
                    if (file2.Offset != file2.Length)
                    {
                        //文件偏移位置,表示从这个位置开始进行后面的数据添加
                        writer = new BinaryWriter(fs); //初始化文件写入器
                        file2  = FileTransportBll.FileDownLoad(file2, system);
                        i      = 0;                    //下载成功,把重试次数初始化
                        while (file2.Data != null)
                        {
                            //打开文件
                            long Offset = file2.Offset; //file.Offset

                            if (Offset >= int.MaxValue)
                            {
                                writer.Seek(int.MaxValue, SeekOrigin.Begin);
                                Offset = Offset - int.MaxValue;
                                while (Offset > 0)
                                {
                                    if (Offset >= int.MaxValue)
                                    {
                                        writer.Seek(int.MaxValue, SeekOrigin.Current);
                                        Offset = Offset - int.MaxValue;
                                    }
                                    else
                                    {
                                        writer.Seek(int.Parse(Offset.ToString()), SeekOrigin.Current);
                                        Offset = 0;
                                    }
                                }
                            }
                            else
                            {
                                writer.Seek(Int32.Parse(Offset.ToString()), SeekOrigin.Begin);//设置文件的写入位置
                            }

                            writer.Write(file2.Data); //写入数据
                            file2.Offset = fs.Length; //返回追加数据后的文件位置
                            file2.Data   = null;
                            file2        = FileTransportBll.FileDownLoad(file2, system);
                            i            = 0;
                            if (HandleProgress != null)
                            {
                                int progress = (int)(((double)file2.Offset / (double)((long)file2.Length)) * 100);
                                HandleProgress(progress);
                            }
                        }
                        writer.Close();
                        fs.Close();

                        if (File.Exists(filePath + ".tz"))
                        {
                            File.Move(filePath + ".tz", filePath);
                        }
                    }
                    return("");
                }
                catch (Exception ex)
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (fs != null)
                    {
                        fs.Close();
                    }
                    // result = false;
                }
            }
            return("下载失败!");
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath">文件的完整</param>
        /// <param name="relativePath">文件的相对路径</param>
        /// <param name="system">系统名称</param>
        /// <returns></returns>
        public bool FileDownLoad(string filePath, string relativePath, SeatManageSubsystem system)
        {
            for (int i = 0; i < 50; i++)
            {
                //获取文件的路径,已经保存的文件名
                FileStream   fs     = null;
                BinaryWriter writer = null;//初始化文件写入器
                try
                {
                    string            fileName = relativePath;// filePath.Substring(filePath.LastIndexOf(@"\") + 1);
                    string            dirPath  = filePath.Substring(0, filePath.LastIndexOf(@"\") + 1);
                    FileSliceInfo     file2    = FileTransportBll.GetFilesSlice_Md5Info(fileName, system);
                    FileSliceInfo_Md5 f        = file2 as FileSliceInfo_Md5;
                    if (file2 == null)
                    {
                        if (DownloadError != null)
                        {
                            DownloadError(string.Format("文件{0}下载失败。", fileName));
                        }
                        return(false);
                    }
                    else if (File.Exists(filePath))
                    {
                        //先设置文件只读属性,然后再删除,不然无法删除文件
                        File.SetAttributes(filePath, System.IO.FileAttributes.Normal);
                        if (f.Md5 == SeatManageComm.SeatComm.GetMD5HashFromFile(filePath))
                        {
                            return(true);
                        }
                    }
                    file2.Name   = fileName;
                    file2.Offset = 0;

                    //如果文件目录不存在,创建文件所存放的目录.
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }

                    //文件先下载,下载完成后再删除
                    fs = new FileStream(filePath + ".tz", FileMode.OpenOrCreate);

                    file2.Offset = fs.Length;
                    if (file2.Offset != file2.Length)
                    {
                        //文件偏移位置,表示从这个位置开始进行后面的数据添加
                        writer = new BinaryWriter(fs);//初始化文件写入器
                        file2  = FileTransportBll.FileDownLoad(file2, system);
                        i      = 0;
                        while (file2.Data != null)
                        {
                            //打开文件
                            long Offset = file2.Offset; //file.Offset

                            if (Offset >= int.MaxValue)
                            {
                                writer.Seek(int.MaxValue, SeekOrigin.Begin);
                                Offset = Offset - int.MaxValue;
                                while (Offset > 0)
                                {
                                    if (Offset >= int.MaxValue)
                                    {
                                        writer.Seek(int.MaxValue, SeekOrigin.Current);
                                        Offset = Offset - int.MaxValue;
                                    }
                                    else
                                    {
                                        writer.Seek(int.Parse(Offset.ToString()), SeekOrigin.Current);
                                        Offset = 0;
                                    }
                                }
                            }
                            else
                            {
                                writer.Seek(Int32.Parse(Offset.ToString()), SeekOrigin.Begin);//设置文件的写入位置
                            }

                            writer.Write(file2.Data); //写入数据
                            file2.Offset = fs.Length; //返回追加数据后的文件位置
                            file2.Data   = null;
                            file2        = FileTransportBll.FileDownLoad(file2, system);
                            i            = 0;
                            if (HandleProgress != null)
                            {
                                int progress = (int)(((double)file2.Offset / (double)((long)file2.Length)) * 100);
                                HandleProgress(progress);
                            }
                        }
                        writer.Close();
                        fs.Close();
                    }
                    else//否则文件已经下载完成,只是还没有更改后缀,则关闭文件流
                    {
                        fs.Close();
                    }
                    if (File.Exists(filePath + ".tz"))
                    {
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                        File.Move(filePath + ".tz", filePath);
                    }
                    return(true);
                }
                catch (Exception ex)
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }
            return(false);
        }