C# FTP 自动创建目录/上传文件

4 篇文章 0 订阅
1 篇文章 0 订阅
C#代码 复制代码  收藏代码
  1. //上传文件   
  2. public static Boolean FtpUpload(string ftpPath,string localFile)   
  3. {   
  4.     //检查目录是否存在,不存在创建   
  5.     FtpCheckDirectoryExist(ftpPath);   
  6.     FileInfo fi = new FileInfo(localFile);   
  7.     FileStream fs = fi.OpenRead();   
  8.     long length = fs.Length;   
  9.     FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpServerIP + ftpPath + fi.Name);   
  10.     req.Credentials = new NetworkCredential(ftpUserID, ftpPassword);   
  11.     req.Method = WebRequestMethods.Ftp.UploadFile;   
  12.     req.ContentLength = length;   
  13.     req.Timeout = 10 * 1000;   
  14.     try  
  15.     {   
  16.         Stream stream = req.GetRequestStream();   
  17.         int BufferLength = 2048; //2K      
  18.         byte[] b = new byte[BufferLength];   
  19.         int i;   
  20.         while ((i = fs.Read(b, 0, BufferLength)) > 0)   
  21.         {   
  22.             stream.Write(b, 0, i);   
  23.         }   
  24.         stream.Close();   
  25.         stream.Dispose();   
  26.     }   
  27.     catch (Exception e)   
  28.     {   
  29.         ErrLog(e.Message + e.StackTrace);   
  30.         return false;   
  31.     }   
  32.     finally  
  33.     {   
  34.         fs.Close();   
  35.         req.Abort();   
  36.     }   
  37.     req.Abort();   
  38.     return true;   
  39. }   
  40.   
  41. //判断文件的目录是否存,不存则创建   
  42. public static void FtpCheckDirectoryExist(string destFilePath)   
  43. {   
  44.     string fullDir = FtpParseDirectory(destFilePath);   
  45.     string[] dirs = fullDir.Split('/');   
  46.     string curDir = "/";   
  47.     for (int i = 0; i < dirs.Length; i++)   
  48.     {   
  49.         string dir = dirs[i];   
  50.         //如果是以/开始的路径,第一个为空     
  51.         if (dir != null && dir.Length > 0)   
  52.         {   
  53.             try  
  54.             {   
  55.                 curDir += dir + "/";   
  56.                 FtpMakeDir(curDir);   
  57.             }   
  58.             catch (Exception)   
  59.             {}   
  60.         }   
  61.     }   
  62. }   
  63.   
  64. public static string FtpParseDirectory(string destFilePath)   
  65. {   
  66.     return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));   
  67. }     
  68.   
  69. //创建目录   
  70. public static Boolean FtpMakeDir(string localFile)   
  71. {   
  72.     FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpServerIP + localFile);   
  73.     req.Credentials = new NetworkCredential(ftpUserID, ftpPassword);   
  74.     req.Method = WebRequestMethods.Ftp.MakeDirectory;   
  75.     try  
  76.     {   
  77.         FtpWebResponse response = (FtpWebResponse)req.GetResponse();   
  78.         response.Close();   
  79.     }   
  80.     catch (Exception)   
  81.     {   
  82.         req.Abort();   
  83.         return false;   
  84.     }   
  85.     req.Abort();   
  86.     return true;   
  87. }  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值