C#实现的Ftp 文件上传与下载类
本文导语: 1,ftp上传与下载类: 代码示例: using System; using System.IO; using System.Net; namespace WindowsFormsApplication1 { public class FtpOption { #region string serverIP; string serverPort; string userI...
1,ftp上传与下载类:
using System;
using System.IO;
using System.Net;
namespace WindowsFormsApplication1
{
public class FtpOption
{
#region
string serverIP;
string serverPort;
string userId;
string passWord;
public FtpOption(string serverIP, string serverPort, string userId, string passWord)
{
this.serverIP = serverIP;
this.serverPort = serverPort ?? "21";
this.userId = userId;
this.passWord = passWord;
}
private FtpWebRequest OpenRequest(Uri uri, string ftpMethord)
{
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
ftpRequest.Credentials = new NetworkCredential(userId, passWord);
ftpRequest.Method = ftpMethord;
ftpRequest.UseBinary = true;
return ftpRequest;
}
catch (Exception ex)
{
throw ex;
}
}
private FtpWebResponse OpenResponse(Uri uri, string ftpMethord)
{
try
{
return this.OpenRequest(uri, ftpMethord).GetResponse() as FtpWebResponse;
}
catch
{
throw new Exception("登录到Ftp服务器失败!");
}
}
#endregion
///
/// 下载(重命名)
///
/// 下载文件全路径
/// 下载到本机全路径(包含文件名)
///
public bool DownLoadReName(string sourceFullPath, string targetFullPath)
{
try
{
Uri uri = new Uri(string.Format("ftp://{0}/{1}", serverIP, sourceFullPath));
FtpWebResponse downloadResponse = OpenResponse(uri, WebRequestMethods.Ftp.DownloadFile);
Stream responseStream = downloadResponse.GetResponseStream();
FileStream fileStream = File.Create(targetFullPath);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while (true)
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
responseStream.Close();
return true;
}
catch
{
throw new Exception("获取下载文件失败!");
}
}
///
/// 下载(按原名直接)
/// www.
/// 下载文件的全路径
/// 下载到指定的地址(E:/)
///
public bool DownLoadNotReName(string sourceFullPath, string targePath)
{
try
{
string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("/"));
return DownLoadReName(sourceFullPath, targePath + fileName);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 文件上传
///
/// 要上传文件的地址(G:/Ftp.txt)
/// 服务器端地址(temp)
///
public bool UploadFile(string sourceFullPath, string targetPath)
{
try
{
string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("/") + 1);
//检查路径
Uri uri = new Uri(string.Format("ftp://{0}:{1}/{2}/{3}", serverIP, serverPort, targetPath, fileName));
FtpWebRequest request = this.OpenRequest(uri, WebRequestMethods.Ftp.UploadFile);
Stream requestStream = request.GetRequestStream();
FileStream fileStream = new System.IO.FileStream(sourceFullPath, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Close();
request.GetResponse();
return true;
}
catch (Exception)
{
return false;
}
}
}
}
2,调用示例
//调用:
FtpOption ftp = new FtpOption("10.XXX.3.90", "21", "uid", "pwd");
//下载文件:
bool success = ftp.DownLoadNotReName("file/201305/2013k4032.gif", "E:/");
bool success = ftp.DownLoadReName("file/201306/2044323o4j53.jpg", "E:/rename.003");
//上传文件:
bool success=ftp.UploadFile("G:/Ftp.txt","temp");