using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Net;
namespace reporting.temp
{
public class FTP
{
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fullName;
private Exception operationException;
string statusCode;
string statusDescription;
string userName;
string password;
string ftpServerIP;
string fileName;
public Exception OperationException
{
get
{
return operationException;
}

set
{
operationException = value;
}
}
public ManualResetEvent OperationComplete
{
get { return wait; }
}
public FtpWebRequest Request
{
get
{
return request;
}

set
{
request = value;
}
}

public string FullName
{
get
{
return fullName;
}

set
{
fullName = value;
}
}

public string StatusCode
{
get
{
return statusCode;
}

set
{
statusCode = value;
}
}

public string StatusDescription
{
get
{
return statusDescription;
}

set
{
statusDescription = value;
}
}

public string UserName
{
get
{
return userName;
}

set
{
userName = value;
}
}

public string Password
{
get
{
return password;
}

set
{
password = value;
}
}

public string FtpServerIP
{
get
{
return ftpServerIP;
}

set
{
ftpServerIP = value;
}
}

public string FileName
{
get
{
return fileName;
}

set
{
fileName = value;
}
}

public FtpState(string ftpServerIP, string userName, string password)
{
FtpServerIP = ftpServerIP;
UserName = userName;
Password = password;
FileName = Path.GetFileName(fullName);
wait = new ManualResetEvent(false);
}

public void AsynchronousFtpUpLoader(string FullName)
{
FtpState AsyncState = new FtpState(FtpServerIP, UserName, Password);
AsyncState.FullName = FullName;
ManualResetEvent waitObject;
Uri target = new Uri("ftp://" + FtpServerIP + "/" + Path.GetFileName(FullName));
//FtpState AsyncState = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
//这个例子使用匿名登录。
//默认情况下,请求是匿名的; 证书不需要指定。
//这个示例仅指定凭据
//控制操作如何在服务器上登录。
request.Credentials = new NetworkCredential(AsyncState.UserName, AsyncState.Password);
//是否指定SSL 连接
request.EnableSsl = false;
//将请求存储在我们传入的对象中
//异步操作。
AsyncState.Request = request;
AsyncState.FullName = AsyncState.FullName;
//让事件继续等待。
waitObject = AsyncState.OperationComplete;
//异步获取文件内容的流。
request.BeginGetRequestStream(
new AsyncCallback(EndGetStreamCallback),
AsyncState
);
//阻塞当前线程,直到所有操作完成为止。
waitObject.WaitOne();
//操作要么完成,要么抛出异常。
if (AsyncState.OperationException != null)
throw AsyncState.OperationException;
else
Console.Write("Done-{0}", AsyncState.StatusDescription);

}

}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState AsyncState = (FtpState)ar.AsyncState;
Stream requestStream;
try
{
requestStream = AsyncState.Request.EndGetRequestStream(ar);
const int bufferLength = 2048;
byte[] _array = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream fs = File.OpenRead(AsyncState.FullName);
do
{
readBytes = fs.Read(_array, 0, bufferLength);
requestStream.Write(_array, 0, readBytes);
count += count;
} while (readBytes != 0);
//将字节写入流
Console.WriteLine("{0}-字节写入流");
fs.Close();
requestStream.Close();
AsyncState.Request.BeginGetResponse(
new AsyncCallback(EndGetResponseCallback),
AsyncState
);
}
catch (Exception e)
{
Console.WriteLine("错误响应:{0}", e.Message);
AsyncState.OperationComplete.Set();
AsyncState.OperationException = e;
throw;
}
}

private static void EndGetResponseCallback(IAsyncResult ar)
{
FtpState AsyncState = (FtpState)ar.AsyncState;
FtpWebResponse response;
try
{
response = (FtpWebResponse)AsyncState.Request.EndGetResponse(ar);
response.Close();
AsyncState.StatusCode = response.StatusCode.ToString();
AsyncState.StatusDescription = response.StatusDescription;
AsyncState.OperationComplete.Set();
}
catch (Exception e)
{
Console.WriteLine("错误响应:{0}", e.Message);
AsyncState.OperationComplete.Set();
AsyncState.OperationException = e;
}
}
}
}

最新文章

  1. 使用postman发送数据并构建collections执行测试
  2. python3 购物程序
  3. HTML5的学习--performance
  4. html中获取图片的真实尺寸
  5. 部门子部门表结构,递归指定部门的所有子部门SQL函数
  6. 记录一次linux线上服务器被黑事件
  7. WebApp开发之Cordova安装教程
  8. assertion的语法和语义
  9. 于PsIsSystemThread无论是在线程系统线程标识获得
  10. 向网页中写入js和css
  11. 201521123051《Java程序设计》第七周学习总结
  12. Eclipse 安装 SVN 插件的两种方法
  13. http缓存(http caching)
  14. 访问iis出现500.21错误
  15. 使用jquery.pjax实现SPA单页面应用
  16. VMware下CentOS7设置网络以及修改系统语言
  17. mybatis之注解式开发
  18. 【BZOJ2558】Count on a tree
  19. zabbix 模板 创建逻辑 + 主动模式-被动模式
  20. Centos7安装FTP突然无法登录

热门文章

  1. 字符串系列——KMP模板整理
  2. POJ2454——Jersey Politics
  3. 洛谷——P1229 遍历问题
  4. 关于Linux字符集的查看及修改
  5. hdu 5122(2014ACM/ICPC亚洲区北京站) K题 K.Bro Sorting
  6. Maven学习总结(31)——Maven坐标详解
  7. 最长上升子序列的回溯 ZOJ 2432
  8. java多线程编程核心技术(一)--多线程技能
  9. 浪潮服务器装机RAID
  10. 洛谷——P1255 数楼梯