• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net实现FTP客户端

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
下载文件
  1. /// <summary>   
  2.         /// 下载文件   
  3.         /// </summary>   
  4.         /// <param name="filename"></param>   
  5.         private static void DownLoadFile(string filename)   
  6.          {   
  7.              FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + "/" + filename);   
  8.              req.Method = WebRequestMethods.Ftp.DownloadFile;   
  9.              req.UseBinary = true;   
  10.              req.UsePassive = true;   
  11.              req.Credentials = new NetworkCredential(FtpUid, FtpPwd);   
  12.             try  
  13.              {   
  14.                 using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())   
  15.                  {   
  16.                     string localfile = Path.Combine(LocalPath, filename);   
  17.                      FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);   
  18.                     int buffer = 1024;  //1K缓冲   
  19.                     byte[] b = new byte[buffer];   
  20.                     int i = 0;   
  21.                      Stream stream = res.GetResponseStream();   
  22.                     while ((i = stream.Read(b, 0, buffer)) > 0)   
  23.                      {   
  24.                          fs.Write(b, 0, i);   
  25.                      }   
  26.                  }   
  27.                  Console.WriteLine(filename + " download!");   
  28.                  Log(filename + "下载成功");   
  29.   
  30.              }   
  31.             catch (Exception ex)   
  32.              {   
  33.                  Console.WriteLine(ex.ToString());   
  34.                  Log(ex.ToString());   
  35.              }   
  36.             finally  
  37.              {   
  38.   
  39.              }   
  40.          }  
/// <summary>         /// 下载文件         /// </summary>         /// <param name="filename"></param>         private static void DownLoadFile(string filename)         {             FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + "/" + filename);             req.Method = WebRequestMethods.Ftp.DownloadFile;             req.UseBinary = true;             req.UsePassive = true;             req.Credentials = new NetworkCredential(FtpUid, FtpPwd);             try             {                 using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())                 {                     string localfile = Path.Combine(LocalPath, filename);                     FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);                     int buffer = 1024;  //1K缓冲                     byte[] b = new byte[buffer];                     int i = 0;                     Stream stream = res.GetResponseStream();                     while ((i = stream.Read(b, 0, buffer)) > 0)                     {                         fs.Write(b, 0, i);                     }                 }                 Console.WriteLine(filename + " download!");                 Log(filename + "下载成功");              }             catch (Exception ex)             {                 Console.WriteLine(ex.ToString());                 Log(ex.ToString());             }             finally             {              }         }


获取文件列表
  1. /// <summary>   
  2.         /// 获取FTP文件列表   
  3.         /// </summary>   
  4.         /// <returns></returns>   
  5.         private static List<String> GetFileList()   
  6.          {   
  7.              List<string> list = new List<string>();   
  8.              FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpAddress + FtpRemotePath));   
  9.              req.Credentials = new NetworkCredential(FtpUid, FtpPwd);   
  10.              req.Method = WebRequestMethods.Ftp.ListDirectory;   
  11.              req.UseBinary = true;   
  12.              req.UsePassive = true;   
  13.             try  
  14.              {   
  15.                 using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())   
  16.                  {   
  17.                     using (StreamReader sr = new StreamReader(res.GetResponseStream()))   
  18.                      {   
  19.                         string s;   
  20.                         while ((s = sr.ReadLine()) != null)   
  21.                          {   
  22.   
  23.                              list.Add(s);   
  24.                          }   
  25.                      }   
  26.                  }   
  27.              }   
  28.             catch (Exception ex)   
  29.              {   
  30.                  Console.WriteLine(ex.ToString());   
  31.                  Log("下载文件列表失败:");   
  32.                  Log(ex.ToString());   
  33.              }   
  34.             return list;   
  35.   
  36.          }  
/// <summary>         /// 获取FTP文件列表         /// </summary>         /// <returns></returns>         private static List<String> GetFileList()         {             List<string> list = new List<string>();             FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpAddress + FtpRemotePath));             req.Credentials = new NetworkCredential(FtpUid, FtpPwd);             req.Method = WebRequestMethods.Ftp.ListDirectory;             req.UseBinary = true;             req.UsePassive = true;             try             {                 using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())                 {                     using (StreamReader sr = new StreamReader(res.GetResponseStream()))                     {                         string s;                         while ((s = sr.ReadLine()) != null)                         {                              list.Add(s);                         }                     }                 }             }             catch (Exception ex)             {                 Console.WriteLine(ex.ToString());                 Log("下载文件列表失败:");                 Log(ex.ToString());             }             return list;          }


上传文件
  1. private static void UploadFile(string localFile)   
  2.          {   
  3.              FileInfo fi = new FileInfo(localFile);   
  4.              FileStream fs = fi.OpenRead();   
  5.             long length = fs.Length;   
  6.              FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);   
  7.              req.Credentials = new NetworkCredential(FtpUid, FtpPwd);   
  8.              req.Method = WebRequestMethods.Ftp.UploadFile;   
  9.              req.UseBinary = true;   
  10.              req.ContentLength = length;   
  11.              req.Timeout = 10 * 1000;   
  12.             try  
  13.              {   
  14.                  Stream stream = req.GetRequestStream();   
  15.                    
  16.                 int BufferLength = 2048; //2K   
  17.                 byte[] b = new byte[BufferLength];   
  18.                 int i;   
  19.                 while ((i = fs.Read(b, 0, BufferLength)) > 0)   
  20.                  {   
  21.                      stream.Write(b, 0, i);   
  22.                  }   
  23.                  stream.Close();   
  24.                  stream.Dispose();   
  25.                    
  26.              }   
  27.             catch (Exception ex)   
  28.              {   
  29.                  Console.WriteLine(ex.ToString());   
  30.              }   
  31.                
  32.          } 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.Net大文件上传组件详解发布时间:2022-07-10
下一篇:
如何在ASP.NETCore中使用Redis发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap