说明:有时候我们遇到要上传文件到FTP网盘的需求,在网上找到了方法,然后我就封装成一个类,方便自己去调用方法,里面的上传的详细也是参考了部分网友的作品,时间有的久了具体引用了哪位程序袁的方法找不到了,共勉。开箱即食,并非最优的方法。(下载FTP文件的方法的后面有空再去研究)
public class FTP
{
/// <summary>
/// 检查FPT是否连接上
/// </summary>
/// <param name="ftpURI"></param>
/// <param name="ftpUserID"></param>
/// <param name="ftpPassword"></param>
/// <returns></returns>
private bool CheckFtp(string ftpURI, string ftpUserID, string ftpPassword)
{
try
{
FtpWebRequest ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
// ftp用户名和密码
ftprequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftprequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftprequest.Timeout = 3000;
FtpWebResponse ftpResponse = (FtpWebResponse)ftprequest.GetResponse();
ftpResponse.Close();
return true;
}
catch (Exception err)
{
return false;
}
}
/*****************接口参数介绍******************/
/// ftpURI为完整的ftp路径例如 ftp://10.24.236.210/CIBU8Test/TestDataBackup/test-cyb/
/// ftpUserID 用户ID
/// ftpPassword 用户密码
/// localfile 本地文件路径 例如 "D:\\FTP-TEST\\" + 文件名
/// ftpfile 上传到ftp时的名字
/// ftpDir 上传时是否要创建文件夹
public bool Upload(string ftpURI, string ftpUserID, string ftpPassword, string localfile, string ftpfile)
{
try
{
FileInfo fileInf = new FileInfo(localfile);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + ftpfile));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
System.Windows.Forms.Application.DoEvents();
}
System.Windows.Forms.Application.DoEvents();
strm.Close();
fs.Close();
return true;
}
catch (Exception err)
{
return false;
}
}
/// <summary>
/// 上传文件到-FTP网盘
/// </summary>
/// <param name="ftpURI">FTP路径 </param>
/// <param name="ftpUserID">账号</param>
/// <param name="ftpPassword">密码</param>
/// <param name="localfile">本地文件路径</param>
/// <param name="ftpfile">上传到网盘的文件名</param>
/// <param name="ftpDir">上传到网盘的文件夹名</param>
/// <returns></returns>
public bool FtpUpLoad(string ftpURI, string ftpUserID, string ftpPassword, string localfile, string ftpfile,string ftpDir = null)
{
try
{
bool result = true;
//检测FTP是否连上
if (CheckFtp(ftpURI, ftpUserID, ftpPassword))
{
if(ftpDir != null)
{
string NewPath = CreatFtpDir(ftpURI, ftpUserID, ftpPassword, ftpDir);
result = Upload(ftpURI, ftpUserID, ftpPassword, localfile, NewPath + ftpfile);
}
else
{
result = Upload(ftpURI, ftpUserID, ftpPassword, localfile, ftpfile);
}
return result;
}
else
{
return false;
}
}
catch (Exception err)
{
return false;
}
}
/// <summary>
/// 在FTP内创建文件夹
/// </summary>
/// <param name="ftpURI"></param>
/// <param name="ftpUserID"></param>
/// <param name="ftpPassword"></param>
/// <param name="ftpDir"></param>
/// <returns></returns>
private string CreatFtpDir(string ftpURI, string ftpUserID, string ftpPassword,string ftpDir = null)
{
try
{
//检测文件夹是否存在,文件夹不存在时创建文件夹
if (CheckDirectory(ftpURI, ftpUserID, ftpPassword, ftpDir)) return ftpDir + "//";
// 创建FTP请求对象
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURI + ftpDir + "\\");
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
// 发送FTP请求
using (WebResponse response = ftpRequest.GetResponse())
{
return ftpDir + "//";
}
return null;
}
catch(Exception err)
{
return null;
}
}
/// <summary>
/// 判断ftp文件夹是否存在
/// </summary>
/// <param name="ftpURI">路径</param>
/// <param name="ftpUserID">账号</param>
/// <param name="ftpPassword">密码</param>
/// <param name="ftpDir">新目录名</param>
/// <returns></returns>
private bool CheckDirectory(string ftpURI, string ftpUserID, string ftpPassword, string ftpDir = null)
{
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURI + ftpDir + "\\");
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
response.Close();
return true;
}
catch (WebException e)
{
if (e.HResult == -2146233079)
{
return false;
}
}
return false;
}
}