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