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

空怀 2010-11-05 03:59:05
如题,怎么做啊,大侠指点~~~
...全文
181 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
  • 打赏
  • 举报
回复
由第三方控件,可直接引用就可以了
内容概要:本文针对无刷直流电机驱动的电子机械制动(EMB)执行器,建立了考虑Stribeck摩擦特性的非线性耦合动力学模型,并在Simulink环境中完成了系统级仿真分析。研究综合集成了电机动力学、齿轮传动机构与制动执行机构的动力学特性,构建了高保真的机电一体化系统模型。重点引入Stribeck摩擦模型以精确描述低速工况下执行器内部存在的静摩擦、粘滞摩擦与库仑摩擦之间的过渡行为,有效提升了系统在启停、反向运动等瞬态过程中的动态响应仿真精度。通过多工况仿真验证了模型的有效性,能够准确反映摩擦引起的爬行、滞后与定位误差等非线性现象,为EMB系统的高性能控制算法设计(如摩擦补偿、滑模控制)与结构优化提供了高可信度的仿真平台。; 适合人群:从事汽车电子制动系统、电机驱动控制、机电系统建模与仿真研究的研究生、科研人员及工程技术人员,需具备扎实的机械动力学、自动控制理论基础和MATLAB/Simulink仿真能力。; 使用场景及目标:①用于高精度电子机械制动系统的设计验证与性能预测;②为消除摩擦非线性影响的先进控制策略(如自适应控制、智能控制)提供精确的被控对象模型;③深入探究Stribeck摩擦等非线性因素对系统动态性能(如响应延迟、稳态误差)的作用机理; 阅读建议:读者应结合提供的Simulink模型文件,深入剖析Stribeck摩擦模块的数学实现与参数辨识方法,建议通过改变输入指令(如阶跃、正弦)和负载条件进行对比仿真,以直观理解非线性摩擦对系统动态特性的影响。

111,129

社区成员

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

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

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