【12】FtpWebRequest上傳下載

來源:互聯網
上載者:User

標籤:use   uri   下載   pos   network   nbsp   列表   pen   line   

下載檔案

 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 }  


擷取檔案清單

 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 }  


上傳檔案

 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 }  

 

【12】FtpWebRequest上傳下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.