当前位置: 编程技术>.net/c#/asp.net
asp.net Ftp操作类(简单入门)
来源: 互联网 发布时间:2014-08-30
本文导语: asp.net ftp操作类。 代码示例: public class FTP { /// /// 从Ftp上下载文件 /// /// 文件下载后的保存路径 /// 源图片名称 /// 本地保存文件名称 /// ...
asp.net ftp操作类。
代码示例:
public class FTP
{
///
/// 从Ftp上下载文件
///
/// 文件下载后的保存路径
/// 源图片名称
/// 本地保存文件名称
/// ftp地址
/// ftp上的文件夹名称
/// 访问ftp的用户名
/// 访问ftp的密码
public void DownLoad(string saveFilePath, string imageSrc, string imageName, string ftpAddress,string folderName, string ftpUserName, string ftpPwd)
{
if (!Directory.Exists(saveFilePath))
{
Directory.CreateDirectory(saveFilePath);
}
using (FileStream outputStream = new FileStream(saveFilePath + "\" + imageName, FileMode.Create))
{
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + imageSrc));
reqFtp.Method = WebRequestMethods.Ftp.DownloadFile;
reqFtp.UseBinary = true;
//ftp访问通行证
reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
using (FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse())
{
using (Stream ftpStream = response.GetResponseStream())
{
long len = response.ContentLength;
int bufferSize = 2048;
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();
}
response.Close();
}
outputStream.Close();
}
}
public void Uplaod(string saveFileDsPath, string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
{
FileInfo fileInfo = new FileInfo(saveFileDsPath);
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + fileInfo.Name));
reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.UseBinary = true;
reqFtp.ContentLength = fileInfo.Length;
int buffSize = 2048;
byte[] buff = new byte[buffSize];
int contentLen = 0;
using (FileStream fs = fileInfo.OpenRead())
{
using (Stream stream = reqFtp.GetRequestStream())
{
contentLen = fs.Read(buff, 0, buffSize);
while (contentLen != 0)
{
stream.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffSize);
}
stream.Close();
}
fs.Close();
}
}
///
/// 删除服务器上的文件
/// by www.
///
/// 文件路径
public void DeleteServerFile(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
///
/// 删除Ftp上的文件
///
/// 删除文件名称集合
/// 被删除文件所属的文件夹
/// ftp地址
/// ftp访问用户名
/// ftp访问密码
private void DeleteFtpFile(string[] Names, string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
{
foreach (string imageName in Names)
{
List fileList = GetFileList(folderName, ftpAddress, ftpUserName, ftpPwd);
foreach (var name in fileList)
{
if (name == imageName)
{
FtpWebRequest reqFtp;
reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + imageName));
reqFtp.Credentials = new NetworkCredential(ftpUserName,ftpPwd);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.DeleteFile;
reqFtp.UseBinary = true;
using(FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse())
{
long size = response.ContentLength;
using(Stream dataStream = response.GetResponseStream())
{
using(StreamReader sr = new StreamReader(dataStream))
{
sr.ReadToEnd();
sr.Close();
}
dataStream.Close();
}
response.Close();
}
}
}
}
}
///
/// 获取特定文件夹中的文件
///
/// 文件夹名称
/// ftp地址
/// 访问ftp的
/// 访问ftp的密码
/// 返回特定文件夹中的文件名称列表
public List GetFileList(string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
{
List downFiles = new List(4);
FtpWebRequest reqFtp;
reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/"));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFtp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string line = reader.ReadLine();
while (line != null)
{
downFiles.Add(line);
line = reader.ReadLine();
}
reader.Close();
response.Close();
//获取图片名称为:1.jpg、2.jpg、3.jpg
var FileList = from file in downFiles
let tempname = file.Substring(0, file.IndexOf('.'))
where tempname == "1" || tempname == "2" || tempname == "3"
select file;
return FileList.ToList();
}
}
{
///
/// 从Ftp上下载文件
///
/// 文件下载后的保存路径
/// 源图片名称
/// 本地保存文件名称
/// ftp地址
/// ftp上的文件夹名称
/// 访问ftp的用户名
/// 访问ftp的密码
public void DownLoad(string saveFilePath, string imageSrc, string imageName, string ftpAddress,string folderName, string ftpUserName, string ftpPwd)
{
if (!Directory.Exists(saveFilePath))
{
Directory.CreateDirectory(saveFilePath);
}
using (FileStream outputStream = new FileStream(saveFilePath + "\" + imageName, FileMode.Create))
{
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + imageSrc));
reqFtp.Method = WebRequestMethods.Ftp.DownloadFile;
reqFtp.UseBinary = true;
//ftp访问通行证
reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
using (FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse())
{
using (Stream ftpStream = response.GetResponseStream())
{
long len = response.ContentLength;
int bufferSize = 2048;
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();
}
response.Close();
}
outputStream.Close();
}
}
public void Uplaod(string saveFileDsPath, string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
{
FileInfo fileInfo = new FileInfo(saveFileDsPath);
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + fileInfo.Name));
reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.UseBinary = true;
reqFtp.ContentLength = fileInfo.Length;
int buffSize = 2048;
byte[] buff = new byte[buffSize];
int contentLen = 0;
using (FileStream fs = fileInfo.OpenRead())
{
using (Stream stream = reqFtp.GetRequestStream())
{
contentLen = fs.Read(buff, 0, buffSize);
while (contentLen != 0)
{
stream.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffSize);
}
stream.Close();
}
fs.Close();
}
}
///
/// 删除服务器上的文件
/// by www.
///
/// 文件路径
public void DeleteServerFile(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
///
/// 删除Ftp上的文件
///
/// 删除文件名称集合
/// 被删除文件所属的文件夹
/// ftp地址
/// ftp访问用户名
/// ftp访问密码
private void DeleteFtpFile(string[] Names, string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
{
foreach (string imageName in Names)
{
List fileList = GetFileList(folderName, ftpAddress, ftpUserName, ftpPwd);
foreach (var name in fileList)
{
if (name == imageName)
{
FtpWebRequest reqFtp;
reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/" + imageName));
reqFtp.Credentials = new NetworkCredential(ftpUserName,ftpPwd);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.DeleteFile;
reqFtp.UseBinary = true;
using(FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse())
{
long size = response.ContentLength;
using(Stream dataStream = response.GetResponseStream())
{
using(StreamReader sr = new StreamReader(dataStream))
{
sr.ReadToEnd();
sr.Close();
}
dataStream.Close();
}
response.Close();
}
}
}
}
}
///
/// 获取特定文件夹中的文件
///
/// 文件夹名称
/// ftp地址
/// 访问ftp的
/// 访问ftp的密码
/// 返回特定文件夹中的文件名称列表
public List GetFileList(string folderName, string ftpAddress, string ftpUserName, string ftpPwd)
{
List downFiles = new List(4);
FtpWebRequest reqFtp;
reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpAddress + "/" + folderName + "/"));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFtp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string line = reader.ReadLine();
while (line != null)
{
downFiles.Add(line);
line = reader.ReadLine();
}
reader.Close();
response.Close();
//获取图片名称为:1.jpg、2.jpg、3.jpg
var FileList = from file in downFiles
let tempname = file.Substring(0, file.IndexOf('.'))
where tempname == "1" || tempname == "2" || tempname == "3"
select file;
return FileList.ToList();
}
}