上一篇主要讲解FTP服务器的搭建,本节话不多说直接撸码:
1.首先ftp需要有ip和port,以及登录用户等信息,因此创建三个属性;
/// <summary>
/// FTP的服务器地址,格式为ftp://192.168.1.234:8021/。
/// </summary>
private string FTPCONSTR { get; set; }
/// <summary>
/// //FTP服务器的用户名
/// </summary>
private string FTPUSERNAME { get; set; }
/// <summary>
/// //FTP服务器的密码
/// </summary>
private string FTPPASSWORD { get; set; }
2.构造函数:
public FtpHhelper(string ip, string username, string password, string port = "8090")
{
FTPCONSTR = string.Format("{0}://{1}:{2}/", "ftp", ip, port);
FTPUSERNAME = username;
FTPPASSWORD = password;
}
3.文件上传方法:
#region 本地文件上传到FTP服务器
/// <summary>
/// 本地文件上传到FTP服务器
/// </summary>
/// <param name="path">本地的文件目录</param>
/// <param name="name">文件名称</param>
/// <returns></returns>
public bool UploadFile(string path, string name)
{
FileInfo f = new FileInfo(path);
path = FTPCONSTR + name;//这个路径是我要传到ftp目录下的这个目录下
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.UsePassive = false;//只需要添加这一句话
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.ContentLength = f.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = f.OpenRead();
try
{
Stream strm = reqFtp.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
4.文件下载方法:
#region FTP服务器文件下载到本地
/// <summary>
/// FTP服务器文件下载到本地
/// </summary>
/// <param name="ftpfileparh"></param>
/// <param name="targetpath"></param>
/// <returns></returns>
private bool DownLoad(string ftpfileparh, string targetpath)
{
bool res;
try
{
FileStream outputStream = new FileStream(targetpath, FileMode.Create);
FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpfileparh));
downRequest.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);
downRequest.Timeout = 10000;
//设置要发送到FTP服务器的命令
downRequest.Method = WebRequestMethods.Ftp.DownloadFile;
downRequest.KeepAlive = true;
//应对ftp下载文件超时处理代码,待测试
downRequest.ServicePoint.Expect100Continue = false;//ftp下载文件时超时处理,继续重新处理
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
downRequest.CachePolicy = noCachePolicy;//清理缓存数据
//
FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();
Stream ftpStream = response.GetResponseStream();
long c1 = response.ContentLength;
int bufferSize = 65536;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception e)
{
return false;
}
return true;
}
#endregion
5.方法调用示例:
///
///
///上传文件用法
///string ftpIP = "192.168.71.249";
///string ftpPort = "65501";
///string ftpUserName = "zy02";
///string ftpPassword = "HYtx@2024";
///string localPath = "D:\\BLL-0001-PZ.pdf";
///string remotePath = "/BLL-0001-PZ.pdf";
///FTPHelper FTPHelper = new FTPHelper(ftpIP, ftpUserName, ftpPassword, ftpPort);
///bool uploadresult = FTPHelper.UploadFile(localPath, remotePath);
///
///
///
/// 下载文件用法
/// //存储临时文件位置
/// string filePath = @"D:\Users\" + ip;
/// //加工程序文件
/// string sourceFile = @"ftp://" + ip;
/// DownLoad(sourceFile+"/"+ proname + ".ini", filePath + "\\" + proname + ".ini");//下载加工文件