两个FTP服务器之间传送数据
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; using System.IO; using System.Text; public class FtpUpDown { string ftpServerIPout; string ftpServerIPin; string ftpUserIDout; string ftpUserIDin; string ftpPasswordout; string ftpPasswordin; FtpWebRequest reqFTPout; FtpWebRequest reqFTPin; public FtpUpDown( string ftpServerIPout, string ftpUserIDout, string ftpPasswordout, string ftpServerIPin, string ftpUserIDin , string ftpPasswordin) { this .ftpServerIPout = ftpServerIPout; this .ftpServerIPin=ftpServerIPin; this .ftpUserIDout = ftpUserIDout; this .ftpUserIDin = ftpUserIDin; this .ftpPasswordout = ftpPasswordout; this .ftpPasswordin = ftpPasswordin; } private void Connect( string pathout, string pathin) //连接ftp { // 根据uri创建FtpWebRequest对象 reqFTPout = (FtpWebRequest)FtpWebRequest.Create( new Uri(pathout)); // 指定数据传输类型 reqFTPout.UseBinary = true ; // ftp用户名和密码 reqFTPout.Credentials = new NetworkCredential(ftpUserIDout, ftpPasswordout); reqFTPout.Method=WebRequestMethods.Ftp.DownloadFile; // 根据uri创建FtpWebRequest对象 reqFTPin = (FtpWebRequest)FtpWebRequest.Create( new Uri(pathin)); // 指定数据传输类型 reqFTPin.UseBinary = true ; // ftp用户名和密码 reqFTPin.Credentials = new NetworkCredential(ftpUserIDin, ftpPasswordin); reqFTPin.Method = WebRequestMethods.Ftp.UploadFile; } public bool Download( string fileName) /**/ ////上面的代码实现了从ftp服务器下载文件的功能 { try { string urlout = "ftp://" + ftpServerIPout + "/" + fileName; string urlin = "ftp://" + ftpServerIPin + "/" + fileName; Connect(urlout, urlin); //连接 FtpWebResponse response = (FtpWebResponse)reqFTPout.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte [] buffer = new byte [bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); Stream strm = reqFTPin.GetRequestStream(); while (readCount != 0) { strm.Write(buffer, 0, bufferSize); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); strm.Close(); response.Close(); return true ; } catch { return false ; } } } 后台调用: protected void Button1_Click( object sender, EventArgs e) { FtpUpDown ftpUpDown = new FtpUpDown( "192.168.0.26" , "tfds" , "tfds" , "192.168.0.34" , "tfds" , "tfds" ); ftpUpDown.Download( "2.txt" ); Response.Write( "success" ); } |
其它相关:
转自:http://www.cnblogs.com/jyshi/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架