C# FTP下载文件夹到本地
来源:互联网 发布:svg js设置 transform 编辑:程序博客网 时间:2024/06/05 23:08
/// <summary> /// FTP文件夹下载到本地文件夹 /// </summary> /// <param name="ftpPath">ftp路径 </param> /// <param name="FtpfolderName">ftp文件夹名称</param> /// <param name="localPath">本地存储路径</param> /// <param name="localfolderName">本地存储文件夹名称</param> /// <param name="userId">ftp用户名</param> /// <param name="password">ftp密码</param> public void DownLoadFTPAllFolder( string ftpPath, string FtpfolderName, string localPath, string localfolderName, string userId, string password) { try { // 本地文件存储路径 string localDownloadPath = localPath + "/" + localfolderName; string FtpUri = ftpPath + (FtpfolderName == null ? null : "/") + FtpfolderName; FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpUri)); reqFTP.Credentials = new NetworkCredential(userId, password); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); long size = response.ContentLength; Stream datastream = response.GetResponseStream(); StreamReader sr = new StreamReader(datastream); string line = sr.ReadLine(); StringBuilder str = new StringBuilder(); while (line != null) { str.Append(line); str.Append("|"); line = sr.ReadLine(); } // 文件夹数据 string[] datas = str.ToString().Split('|'); for (int i = 0; i < datas.Length; i++) { if (datas[i].Contains("<DIR>")) { int index = datas[i].IndexOf("<DIR>"); string name = datas[i].Substring(index + 5).Trim(); // 创建文件夹 // 文件夹不存在的场合 if (!Directory.Exists(localDownloadPath + "/" + name)) { // 创建文件夹 Directory.CreateDirectory(localDownloadPath + "/" + name); } // 回调 DownLoadFTPAllFolder(FtpUri, name, localDownloadPath, name, userId, password); } else { // 文件为空的场合 if (!string.IsNullOrEmpty(datas[i].Trim())) { string fileName = datas[i].Substring(datas[i].LastIndexOf(" ") + 1); // ftp文件路径 string ftpfilePath = FtpUri + "/" + fileName; // 本地文件路径 string localfilePath = localDownloadPath + "/" + fileName; // 读取FTP文件流 //FileMode.Create如果文件已存在,它将被改写 // 本地文件流 FileStream outputStream = new FileStream(localfilePath, FileMode.Create); FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpfilePath)); //设置要发送到 FTP 服务器的命令 downRequest.Credentials = new NetworkCredential(userId, password); downRequest.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse responseStream = (FtpWebResponse)downRequest.GetResponse(); // ftp文件流 Stream ftpStream = responseStream.GetResponseStream(); long cl = responseStream.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(); outputStream.Close(); responseStream.Close(); } } } sr.Close(); datastream.Close(); response.Close(); } catch (Exception ex) { throw ex; } } /// <summary> /// 下载FTP文件 /// </summary> /// <param name="userId">userId</param> /// <param name="password">password</param> /// <param name="ftpfilePath">ftpfilePath</param> /// <param name="localfilePath">localfilePath</param> private void downLoadftpFile(string userId, string password, string ftpfilePath, string localfilePath) { // 读取FTP文件流 //FileMode.Create如果文件已存在,它将被改写 // 本地文件流 FileStream outputStream = new FileStream(localfilePath, FileMode.Create); FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpfilePath)); //设置要发送到 FTP 服务器的命令 downRequest.Credentials = new NetworkCredential(userId, password); downRequest.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse responseStream = (FtpWebResponse)downRequest.GetResponse(); // ftp文件流 Stream ftpStream = responseStream.GetResponseStream(); long cl = responseStream.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(); outputStream.Close(); responseStream.Close(); }
文章可能还有疏落的地方,请大家有问题指教以下。
阅读全文
1 0
- C# FTP下载文件夹到本地
- C# 从FTP服务器下载文件到本地
- FTP下载文件夹到windows
- C# 遍历FTP文件夹/下载
- C# FTP 文件 文件夹操作 上传 下载
- C# FTP 文件 文件夹操作 上传 下载
- java读取FTP文件下载到本地
- FTP服务端文件下载到本地
- Android 从 ftp下载图片到本地
- C# 下载图片到本地
- 如何用命令行上传/下载整个文件夹到ftp服务器?
- C# FTP下载一目录下所有文件夹及文件
- C# FTP下载一目录下所有文件夹及文件
- C# 下载FTP服务器一个文件夹内的所有文件
- 批处理完成FTP下载远程备份文件到本地
- java从ftp上下载图片到客户端本地
- java 远程从FTP服务器下载文件到本地
- ssm单文件下载ftp服务器到本地
- android项目依赖aar的方式
- NSNotification tips
- 热修复框架Tinker的从0到集成之路
- Cesium学习笔记(一)添加实体
- 动态计算UITableViewCell高度详解
- C# FTP下载文件夹到本地
- scrollView嵌套webview+listview listview显示不全 上拉listview加载更多
- Xcode 6视图调试
- getCurrentDate
- Android中通过Intent传递对象、集合问题
- Android-完美解决在Activity中触摸返回键onBackPressed不能触发问题
- GCD使用经验与技巧浅谈
- IFeatureLayerDefinition接口查询要素图层方法汇总
- 802.11协议下,分组从发送到接收的全过程(信道到物理层到MAC层传输过程)