示例#1
0
        /// <summary>
        /// 下载FTP文件
        /// </summary>
        /// <param name="part"></param>
        void DownloadFTPFile(string key, FTPItem part, double filesize)
        {
            try
            {
                //下载文件的URI
                Uri u = new Uri(part.ServerFilePath);
                //设定下载文件的保存路径
                string downFile = part.LocFilePath;



                //FtpWebRequest的作成
                System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(u);

                //设定用户名和密码

                ftpReq.Credentials = new System.Net.NetworkCredential(FTPUser, FTPPassword);

                //MethodにWebRequestMethods.Ftp.DownloadFile("RETR")设定

                ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;

                //要求终了后关闭连接
                ftpReq.KeepAlive = false;

                //使用ASCII方式传送
                ftpReq.UseBinary = false;

                //设定PASSIVE方式无效
                ftpReq.UsePassive = false;



                //判断是否继续下载
                //继续写入下载文件的FileStream
                System.IO.FileStream fs;
                if (part.IsContinue)
                {
                    if (System.IO.File.Exists(downFile))
                    {
                        //继续下载
                        ftpReq.ContentOffset = (new System.IO.FileInfo(downFile)).Length;
                        fs = new System.IO.FileStream(downFile, System.IO.FileMode.Append, System.IO.FileAccess.Write);
                    }

                    else
                    {
                        //一般下载
                        fs = new System.IO.FileStream(downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    }
                }
                else
                {
                    if (System.IO.File.Exists(downFile))
                    {
                        File.Delete(downFile);
                    }
                    //一般下载
                    fs = new System.IO.FileStream(downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                }



                //取得FtpWebResponse
                System.Net.FtpWebResponse ftpRes = (System.Net.FtpWebResponse)ftpReq.GetResponse();

                //为了下载文件取得Stream
                System.IO.Stream resStrm = ftpRes.GetResponseStream();

                //写入下载的数据
                byte[] buffer = new byte[20480];

                double posLen = 0;
                while (part.CloseState == false && part.RunState == FTPRunState.Run)
                {
                    int readSize = resStrm.Read(buffer, 0, buffer.Length);
                    posLen += readSize;
                    if (readSize == 0)
                    {
                        break;
                    }

                    fs.Write(buffer, 0, readSize);

                    if (OnProgressChanged != null)
                    {
                        //System.Threading.Tasks.Task.Factory.StartNew(() =>
                        //{
                        OnProgressChanged(key, Math.Round((posLen / filesize) * 100, 2));
                        //});
                    }
                }
                fs.Close();
                resStrm.Close();

                if (part.RunState == FTPRunState.Run)
                {
                    UploadFileList.TryRemove(key, out part);
                    if (OnProgressChanged != null)
                    {
                        OnProgressChanged(key, 100.00);
                    }

                    if (OnCompleted != null)
                    {
                        OnCompleted(key, part.ServerFilePath);
                    }
                }


                //表示从FTP服务器被送信的状态
                //Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription);

                //关闭连接
                ftpRes.Close();
            }
            catch (Exception ex)
            {
                part.RunState = FTPRunState.Error;
                UploadFileList.TryRemove(key, out part);//出错则删除
                OnFailed(key, 150);
            }
        }
示例#2
0
        /// <summary>
        /// 上传FTP文件
        /// </summary>
        /// <param name="part"></param>
        void UploadFTPFile(string key, FTPItem part)
        {
            //bool success = true;
            FileInfo fileInf = new FileInfo(part.LocFilePath);
            long     allbye  = (long)fileInf.Length;

            long startfilesize = 0;

            if (part.IsContinue)
            {
                startfilesize = GetFtpFileSize(part.ServerFilePath, FTPUser, FTPPassword);// GetFileSize(newFileName, ftpServerIP, ftpUserID, ftpPassword, path);
            }
            if (startfilesize >= allbye)
            {
                return;
            }
            long startbye = startfilesize;

            string        uri = part.ServerFilePath;
            FtpWebRequest reqFTP;

            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(FTPUser, FTPPassword);
            // 默认为true,连接不会被关闭
            // 在一个命令之后被执行
            reqFTP.KeepAlive = false;
            // 指定执行什么命令
            if (part.IsContinue)
            {
                reqFTP.Method = WebRequestMethods.Ftp.AppendFile;
            }
            else
            {
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            }
            // 指定数据传输类型
            reqFTP.UseBinary = true;
            // 上传文件时通知服务器文件的大小
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 20480;// 缓冲大小设置为20kb

            byte[] buff = new byte[buffLength];
            // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
            FileStream fs = fileInf.OpenRead();

            try
            {
                // 把上传的文件写入流
                Stream strm = reqFTP.GetRequestStream();
                // 每次读文件流的20kb
                fs.Seek(startfilesize, SeekOrigin.Begin);
                int contentLen = fs.Read(buff, 0, buffLength);
                // 流内容没有结束
                while (contentLen != 0 && part.CloseState == false && part.RunState == FTPRunState.Run)
                {
                    // 把内容从file stream 写入 upload stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                    startbye  += contentLen;
                    if (OnProgressChanged != null)
                    {
                        //System.Threading.Tasks.Task.Factory.StartNew(() =>
                        //{
                        OnProgressChanged(key, Math.Round(((double)startbye / (double)reqFTP.ContentLength) * 100, 2));
                        //});
                    }
                }
                // 关闭两个流
                strm.Close();
                fs.Close();

                if (part.RunState == FTPRunState.Run)
                {
                    UploadFileList.TryRemove(key, out part);
                    if (OnProgressChanged != null)
                    {
                        OnProgressChanged(key, 100);
                    }
                    if (OnCompleted != null)
                    {
                        OnCompleted(key, part.ServerFilePath);
                    }
                }
            }
            catch (Exception ex)
            {
                part.RunState = FTPRunState.Error;
                UploadFileList.TryRemove(key, out part);//出错则删除
                OnFailed(key, 150);
            }
        }