c#上传到ftp服务器 下载~~~

空怀 2010-11-05 03:59:05
如题,怎么做啊,大侠指点~~~
...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
huayy 2010-11-06
  • 打赏
  • 举报
回复
这种常见问题,你搜索一下孟子E章~
yanele 2010-11-06
  • 打赏
  • 举报
回复
好东东,先收藏了!
xiaoxuan_5612 2010-11-06
  • 打赏
  • 举报
回复
一页放不下,继续

/// <summary>
/// 删除FTP中的某个文件
/// </summary>
/// <param name="ftpPath">待删除文件在FTP中的相对根目录的路径,不包括文件名</param>
/// <param name="ftpName">待删除文件的文件名</param>
/// <returns>是否成功删除文件</returns>
public bool fileDelete(string ftpPath, string ftpName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
FtpWebResponse ftpWebResponse = null;
Stream ftpResponseStream = null;
StreamReader streamReader = null;
try
{
string uri = ftpRootURL + ftpPath + ftpName;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.KeepAlive = false;
ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile;
ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
long size = ftpWebResponse.ContentLength;
ftpResponseStream = ftpWebResponse.GetResponseStream();
streamReader = new StreamReader(ftpResponseStream);
string result = String.Empty;
result = streamReader.ReadToEnd();
success = true;
}
catch (Exception)
{
success = false;
}
finally
{
if (streamReader != null)
{
streamReader.Close();
}
if (ftpResponseStream != null)
{
ftpResponseStream.Close();
}
if (ftpWebResponse != null)
{
ftpWebResponse.Close();
}
}
return success;
}

/// <summary>
/// 检查文件是否存在
/// </summary>
/// <param name="ftpPath">待检查文件在FTP中的相对根目录的路径,不包括文件名</param>
/// <param name="ftpName">待检查文件的文件名</param>
/// <returns>待检查的文件是否存在</returns>
public bool fileCheckExist(string ftpPath, string ftpName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
WebResponse webResponse = null;
StreamReader reader = null;
try
{
string url = ftpRootURL + ftpPath;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpWebRequest.KeepAlive = false;
webResponse = ftpWebRequest.GetResponse();
reader = new StreamReader(webResponse.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (line == ftpName)
{
success = true;
break;
}
line = reader.ReadLine();
}
}
catch (Exception)
{
success = false;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (webResponse != null)
{
webResponse.Close();
}
}
return success;
}

//TODO:增加在FTP中创建、删除、重命名文件夹,检查某文件夹是否存在等操作
}



作者:姜敏@信研所
xiaoxuan_5612 2010-11-06
  • 打赏
  • 举报
回复

using System;
using System.Net;
using System.IO;

public class FTPClient
{
private string ftpUser = String.Empty;
private string ftpPassword = String.Empty;
private string ftpRootURL = String.Empty;

/// <summary>
/// 构造文件上传实例
/// </summary>
/// <param name="url">FTP服务器的根地址,可以使IP地址,也可以是域名</param>
/// <param name="userid">FTP用户名</param>
/// <param name="password">FTP密码</param>
public FTPClient(string url, string userid, string password)
{
this.ftpUser = userid;
this.ftpPassword = password;
this.ftpRootURL = "ftp://" + url + "/";
}

/// <summary>
/// 上传文件到FTP
/// </summary>
/// <param name="localFile">本地待上传的文件</param>
/// <param name="ftpPath">上传到FTP中的相对根目录的路径</param>
/// <param name="ftpFileName">文件在FTP上的文件名</param>
/// <returns>是否成功上传</returns>
public bool fileUpload(FileInfo localFile, string ftpPath, string ftpFileName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
FileStream localFileStream = null;
Stream requestStream = null;
try
{
string uri = ftpRootURL + ftpPath + ftpFileName;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.UseBinary = true;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.ContentLength = localFile.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
localFileStream = localFile.OpenRead();
requestStream = ftpWebRequest.GetRequestStream();
contentLen = localFileStream.Read(buff, 0, buffLength);
while (contentLen != 0)
{
requestStream.Write(buff, 0, contentLen);
contentLen = localFileStream.Read(buff, 0, buffLength);
}
success = true;
}
catch (Exception)
{
success = false;
}
finally
{
if (requestStream != null)
{
requestStream.Close();
}
if (localFileStream != null)
{
localFileStream.Close();
}
}
return success;
}

/// <summary>
/// 上传文件到FTP
/// </summary>
/// <param name="localPath">本地待上传文件的完全地址</param>
/// <param name="ftpPath">上传到FTP中的相对根目录的路径</param>
/// <param name="ftpFileName">上传到FTP上的文件名,包括文件的扩展名</param>
/// <returns>是否成功上传</returns>
public bool fileUpload(string localPath, string ftpPath, string ftpFileName)
{
bool success = false;
try
{
FileInfo localFile = new FileInfo(localPath);
if (localFile.Exists)
{
success = fileUpload(localFile, ftpPath, ftpFileName);
}
else
{
success = false;
}
}
catch (Exception)
{
success = false;
}
return success;
}

/// <summary>
/// 下载文件到本地
/// </summary>
/// <param name="localPath">待下载的文件在本地的完整路径,不包括文件名</param>
/// <param name="localFileName">待下载的文件在本地的文件名,包括文件的扩展名</param>
/// <param name="ftpPath">待下载文件在FTP上的相对根目录的路径</param>
/// <param name="ftpFileName">待下载问及那在FTP上的文件名</param>
/// <returns>是否成功下载</returns>
public bool fileDownload(string localPath, string localFileName, string ftpPath, string ftpFileName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
FtpWebResponse ftpWebResponse = null;
Stream ftpResponseStream = null;
FileStream outputStream = null;
try
{
outputStream = new FileStream(localPath + localFileName, FileMode.Create);
string uri = ftpRootURL + ftpPath + ftpFileName;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.UseBinary = true;
ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
ftpResponseStream = ftpWebResponse.GetResponseStream();
long contentLength = ftpWebResponse.ContentLength;
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
int readCount;
readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
}
success = true;
}
catch (Exception)
{
success = false;
}
finally
{
if (outputStream != null)
{
outputStream.Close();
}
if (ftpResponseStream != null)
{
ftpResponseStream.Close();
}
if (ftpWebResponse != null)
{
ftpWebResponse.Close();
}
}
return success;
}

/// <summary>
/// 给FTP中的文件重命名
/// </summary>
/// <param name="ftpPath">待重命名的文件在FTP中的相对根目录的路径,不包括文件名</param>
/// <param name="currentFileName">待重命名的文件在FTP中的原文件名</param>
/// <param name="newFileName">待重命名的文件在FTP中的新文件名</param>
/// <returns>是否成功重命名</returns>
public bool fileRename(string ftpPath, string currentFileName, string newFileName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
FtpWebResponse ftpWebResponse = null;
Stream ftpResponseStream = null;
try
{
string uri = ftpRootURL + ftpPath + currentFileName;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.UseBinary = true;
ftpWebRequest.Method = WebRequestMethods.Ftp.Rename;
ftpWebRequest.RenameTo = newFileName;
ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
ftpResponseStream = ftpWebResponse.GetResponseStream();
}
catch (Exception)
{
success = false;
}
finally
{
if (ftpResponseStream != null)
{
ftpResponseStream.Close();
}
if (ftpWebResponse != null)
{
ftpWebResponse.Close();
}
}
return success;
}


作者:姜敏@信研所
linghubo 2010-11-06
  • 打赏
  • 举报
回复
由第三方控件,可直接引用就可以了

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧