注意使用下面方法前,将FTP搭建完成,并配置相应的权限。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace Linkwin.Common.Helper
{
public class FtpHelper
{
/// <summary>
/// 登录名
/// </summary>
private string UserName = "";
/// <summary>
/// 登陆密码
/// </summary>
private string PWD = "";
public FtpHelper(string userName,string pwd)
{
UserName = userName;
PWD = pwd;
}
// <summary>
/// 上传文件到FTP服务器.
/// </summary>
public void UploadByFtp(string ftpAddress, string filePath, string name)
{
FileStream fs = null;
Stream stream = null;
string uri = string.Empty;
FileInfo fi = new FileInfo(filePath);
if (ftpAddress.Contains("ftp://"))
uri = ftpAddress + "/" + name;
else
uri = "ftp://" + "/" + ftpAddress + name;
try
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
request.Credentials = new NetworkCredential(UserName, PWD);
request.EnableSsl = true;//使用SSl
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.ContentLength = fi.Length;
byte[] buffer = new byte[2048];
fs = fi.OpenRead();
stream = request.GetRequestStream();
int len = fs.Read(buffer, 0, buffer.Length);
while (len != 0)
{
stream.Write(buffer, 0, len);
len = fs.Read(buffer, 0, buffer.Length);
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (stream != null)
stream.Close();
if (fs != null)
fs.Close();
}
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
/// <returns>判断FTP路径是否存在 0: exist; -1: not exist; 1: unknown (other cause)</returns>
public int IsExist(string ftpAddress, string parentDirector, string directory)
{
string uri = string.Empty;
int result;
if (ftpAddress.Contains("ftp://"))
uri = ftpAddress;
else
uri = "ftp://" + ftpAddress;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri + parentDirector + directory));
request.Credentials = new NetworkCredential(UserName, PWD);
request.EnableSsl = true;//使用SSl
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.ListDirectory;
try
{
request.GetResponse().Close();
result = 0;
}
catch (WebException we)
{
if (((FtpWebResponse)we.Response).StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
result = -1;
}
else
{
result = 1;
throw we;
}
}
catch (Exception e)
{
result = 1;
throw e;
}
return result;
}
/// <summary>
/// 在FTP服务器创建目录.
/// </summary>
public void CreateDirectory(string ftpAddress, string parentDirector, string directory)
{
try
{
string uri = string.Empty;
if (ftpAddress.Contains("ftp://"))
uri = ftpAddress;
else
uri = "ftp://" + ftpAddress;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri + parentDirector + directory));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(UserName, PWD);
reqFTP.EnableSsl = true;//使用SSl
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
Log.LogHelper.Info(ex.Message, "");
}
}
}
}