ftp文件上传和下载
2013-09-24 19:57
627 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Threading; using System.IO; namespace FtpHelper { public delegate void UploadCompleteHandler(); public delegate void UploadProgressHandler(int bytesRead, int totalBytes); public class FtpUpload { public UploadCompleteHandler CompleteCallback; public UploadProgressHandler ProgressCallback; private FtpWebRequest reqFTP; private FtpWebResponse resFTP; private Thread mThread = null; //本地文件路径 private string sFileName = string.Empty; public string UlFileName { get { return sFileName; } set { sFileName = value; } } private Stream uploadStream = null; public bool IsComplete = false; //用于终止上传 private bool stopFlag = false; //上传到Ftp路径,示例:"ftp://192.168.2.200/Project/asss.xls" private string sFtpUrl = string.Empty; private string sFtpUser = string.Empty; private string sFtpPassword = string.Empty; private int BytesProcessed; public FtpUpload(string sUrlPath, string ftpUser, string ftpPassword) { sFtpUrl = sUrlPath; sFtpUser = ftpUser; sFtpPassword = ftpPassword; } /// <summary> /// 后台上传 /// </summary> public void UploadBackgroundFile() { if (CompleteCallback == null) throw new ArgumentException("未定义上传成功后执行的回调函数!"); //实例化下载线程 mThread = new Thread(new ThreadStart(Upload)); mThread.Name = "upload"; mThread.IsBackground = true; mThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture; //开启后台下载线程 mThread.Start(); } protected void Upload() { using (FileStream fileStream = new FileStream(sFileName, FileMode.Open)) { byte[] fsdata = new byte[Convert.ToInt32(fileStream.Length)]; fileStream.Read(fsdata, 0, Convert.ToInt32(fileStream.Length)); try { if (stopFlag) { IsComplete = true; return; } reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(sFtpUrl)); reqFTP.UseBinary = true; reqFTP.Proxy = null; reqFTP.UsePassive = false; reqFTP.Credentials = new NetworkCredential(sFtpUser, sFtpPassword); if (stopFlag) { IsComplete = true; return; } reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.ContentLength = fileStream.Length; int buffLength = 2048; byte[] buffer = new byte[buffLength]; uploadStream = reqFTP.GetRequestStream(); fileStream.Position = 0; while (true) { if (stopFlag) { IsComplete = true; return; } int bytesRead = fileStream.Read(buffer, 0, buffLength); if (bytesRead <= 0) break; uploadStream.Write(buffer, 0, bytesRead); BytesProcessed += bytesRead; OnProgressCallback(BytesProcessed, (int)reqFTP.ContentLength); } OnCompleteCallback(); } catch (Exception ex) { if (fileStream != null) { fileStream.Close(); //fileStream = null; } Logger.Log.Write(ex.Message); //throw; } finally { if (uploadStream != null) { uploadStream.Close(); uploadStream.Dispose(); } reqFTP = null; IsComplete = true; } } } private void OnProgressCallback(int bytesRead, int totalBytes) { if (ProgressCallback != null) { ProgressCallback(bytesRead, totalBytes); } } private void OnCompleteCallback() { if (CompleteCallback != null) { CompleteCallback(); } } /// 终止当前下载 /// </summary> public void Cancel() { CompleteCallback = null; ProgressCallback = null; if (mThread != null && mThread != Thread.CurrentThread) { if (mThread.IsAlive) { // Log.Write(Log.Levels.Verbose, "WebDownload.Cancel() : stopping download thread..."); stopFlag = true; if (!mThread.Join(500)) { //Log.Write(Log.Levels.Warning, "WebDownload.Cancel() : download thread refuses to die, forcing Abort()"); mThread.Abort(); } } mThread = null; } } public void Dispose() { if (mThread != null && mThread != Thread.CurrentThread) { if (mThread.IsAlive) { // Log.Write(Log.Levels.Verbose, "WebDownload.Dispose() : stopping download thread..."); stopFlag = true; if (!mThread.Join(500)) { //Log.Write(Log.Levels.Warning, "WebDownload.Dispose() : download thread refuses to die, forcing Abort()"); mThread.Abort(); } } mThread = null; } if (reqFTP != null) { reqFTP.Abort(); reqFTP = null; } //if (DownloadStartTime != DateTime.MinValue) // OnDebugCallback(this); GC.SuppressFinalize(this); } } }
View Code
相关文章推荐
- java实现ftp的文件上传和下载
- dos下ftp上传下载文件
- 不用FTP使用SecureCRT上传下载文件,并解决rz、sz command not found异常
- ftp 实现文件的上传下载以及列出文件列表
- FTP上传下载文件
- FTP文件上传与下载
- 如何在命令行中使用 ftp 命令上传和下载文件
- 用FTPClient对ftp文件进行上传下载等操作
- java ftp 文件的上传和下载 亲测可用
- net2ftp在线ftp上传、下载、压缩/解压缩文件
- java FTP上传下载文件
- java操作FTP,实现文件上传下载删除操作
- Linux_ftp_命令行下下载文件get与上传文件put的命令应用
- JAVA FTP文件上传下载删除
- JAVA中使用FTPClient实现文件上传下载实例代码
- JAVA中使用FTPClient实现文件上传下载
- 使用sun.net.ftp下载、上传文件完整版
- python实现的简单FTP上传下载文件实例
- ProxySG 使用FTP代理模式上传下载大文件缓慢
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等