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;
}
}
}
}

原文地址:https://www.cnblogs.com/rancrazy/p/7838873.html