Exemplo n.º 1
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型</param>
        /// <param name="fileName">FTP文件名</param>
        /// <param name="filePath">本地文件路径</param>
        /// <param name="overwrite">是否覆盖文件</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        public static bool CF_DownloadFile(ModFtpInfomation ftpInfomation, string fileName, string filePath, bool overwrite, out string errMsg)
        {
            bool result = false;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                //判断FTP文件是否存在
                EExistence isExistFile = CF_IsExistFile(ftpInfomation, fileName, out errMsg);
                if (isExistFile == EExistence.NotExist)
                {
                    errMsg = "FTP文件不存在!";
                    result = false;
                }
                else if (isExistFile == EExistence.Exist)
                {
                    FileInfo file = new FileInfo(filePath);

                    //创建本地文件夹
                    if (!file.Directory.Exists)
                    {
                        file.Directory.Create();
                    }

                    bool isExist = file.Exists;

                    if (isExist)
                    {
                        //是否覆盖
                        if (!overwrite)
                        {
                            errMsg = "目标文件已存在!";
                            result = false;
                        }
                        else
                        {
                            isExist = false;
                        }
                    }

                    if (!isExist)
                    {
                        ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.DownloadFile;
                        ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(fileName);
                        FtpWebResponse ftpWebResponse = CF_GetFtpResponse(ftpInfomation, out errMsg);

                        if (ftpWebResponse != null)
                        {
                            using (Stream stream = ftpWebResponse.GetResponseStream())
                            {
                                using (FileStream fileStream = new FileStream(file.FullName, FileMode.Create, FileAccess.Write))
                                {
                                    ModTransmissionSpeed transmissionSpeed = ftpInfomation.FtpReqInfo.TransmissionSpeed;
                                    if (transmissionSpeed.EnableLimit)
                                    {
                                        //缓存字节数
                                        int bufferSize = transmissionSpeed.Speed * (int)Math.Pow(2, transmissionSpeed.Unit.CF_ToNumber());
                                        //缓存
                                        byte[] btBuffer = new byte[bufferSize];

                                        //读取的字节数
                                        int readSize = stream.Read(btBuffer, 0, bufferSize);
                                        while (readSize > 0)
                                        {
                                            //写入服务器
                                            fileStream.Write(btBuffer, 0, readSize);

                                            //延时
                                            Thread.Sleep(transmissionSpeed.Delay);

                                            readSize = stream.Read(btBuffer, 0, bufferSize);
                                        }
                                    }
                                    else
                                    {
                                        stream.CopyTo(fileStream);
                                    }
                                }
                            }
                        }

                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = false;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(目标文件路径)</param>
        /// <param name="fileName">FTP文件名</param>
        /// <param name="filePath">本地文件路径</param>
        /// <param name="overwrite">是否覆盖文件</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        public static bool CF_UploadFile(ModFtpInfomation ftpInfomation, string fileName, string filePath, bool overwrite, out string errMsg)
        {
            bool result = false;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                //判断原始文件是否存在
                FileInfo file = new FileInfo(filePath);
                if (!file.Exists)
                {
                    errMsg = "本地文件不存在!";
                    result = false;
                }
                else
                {
                    EExistence isExistDir = CF_IsExistDirectory(ftpInfomation, out errMsg);
                    if (isExistDir == EExistence.Exist || string.IsNullOrEmpty(ftpInfomation.GetCurrentName()))
                    {
                        //判断目标文件是否存在
                        EExistence isExistFile = CF_IsExistFile(ftpInfomation, fileName, out errMsg);
                        if (isExistFile == EExistence.Exist)
                        {
                            //是否覆盖
                            if (!overwrite)
                            {
                                errMsg = "目标文件已存在!";
                                result = false;
                            }
                            else
                            {
                                isExistFile = EExistence.NotExist;
                            }
                        }

                        if (isExistFile == EExistence.NotExist)
                        {
                            ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.UploadFile;
                            ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(fileName);
                            FtpWebRequest ftpWebRequest = CF_GetFtpRequest(ftpInfomation, out errMsg);

                            if (ftpWebRequest != null)
                            {
                                using (Stream stream = ftpWebRequest.GetRequestStream())
                                {
                                    using (FileStream fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
                                    {
                                        ModTransmissionSpeed transmissionSpeed = ftpInfomation.FtpReqInfo.TransmissionSpeed;
                                        if (transmissionSpeed.EnableLimit)
                                        {
                                            //缓存字节数
                                            int bufferSize = transmissionSpeed.Speed * (int)Math.Pow(2, transmissionSpeed.Unit.CF_ToNumber());
                                            //缓存
                                            byte[] btBuffer = new byte[bufferSize];

                                            //读取的字节数
                                            int readSize = fileStream.Read(btBuffer, 0, bufferSize);
                                            while (readSize > 0)
                                            {
                                                //写入本地文件
                                                stream.Write(btBuffer, 0, readSize);

                                                //延时
                                                Thread.Sleep(transmissionSpeed.Delay);

                                                readSize = fileStream.Read(btBuffer, 0, bufferSize);
                                            }
                                        }
                                        else
                                        {
                                            fileStream.CopyTo(stream);
                                        }
                                    }
                                }
                            }

                            result = true;
                        }
                    }
                    else
                    {
                        errMsg = "目标文件夹不存在!";
                        result = false;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = false;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }