下载ftp服务器上指定文件

时间:2013-10-7    作者:悬浮的青春    分类: .net相关


文笔不好。话不多说。直接贴代码吧。。

 

如下:

 string FtpIP = "";
            string FtpPort = "";
            string ReportFilePath = "";//

            string SourceFilePath = "";
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://"+FtpIP+":"+FtpPort+"" +
                             ReportFilePath
                );//完整路径
            ftpRequest.Credentials = new NetworkCredential("帐号", "密码");
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            stream = ftpResponse.GetResponseStream();
            string path=@"C:\Temp\"+DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")+".zip";
            System.IO.FileStream localFileStream = new System.IO.FileStream(path, FileMode.Create);
            byte[] byteBuffer = new byte[2048];
            int bytesRead = stream.Read(byteBuffer, 0, 2048);
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = stream.Read(byteBuffer, 0, 2048);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            localFileStream.Close();
            stream.Close();
            ftpResponse.Close();
            ftpRequest = null;

下载完毕

标签: net

WRITTEN BY

avatar