【C#随手记】FTP上传与下载
2022/1/11 9:34:14
本文主要是介绍【C#随手记】FTP上传与下载,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
完整代码:
using System.IO; using System.Net; /// <summary> //——————————参数示例 //private string FTPIpPath = "ftp://119.45.5.197:21/";//默认端口号21 //private string UserName = "Njiyue";//用户名 //private string Password = "000000";//密码 //private string FTPFolderPath = "DigitalMediaInteractiveSystem/";//注意:IP后接的不是“/www/wwwroot/”根目录,而是FTP目录; /// </summary> public static class FTPUploadAndDownload { /// <summary> /// FTP上传 注意设置读写FTP权限 /// </summary> /// <param name="LoadFilePath">本地文件的路径</param> /// <param name="FTPPath">要上传到的FTP文件夹路径</param> public static void _____UpLoadFile(FileInfo fileInfo, string FTPPath, string userName, string password) { //FileInfo fileInfo = new FileInfo(LoadFilePath); FtpWebRequest request = CreateFtpWebRequest(FTPPath + fileInfo.Name, userName, password, WebRequestMethods.Ftp.UploadFile); int buffLength = 2048;//一次要读取和写入多少个字节 byte[] buff = new byte[buffLength];//写入的字节 int contentLen; FileStream fs = fileInfo.OpenRead();//读取文件,将文件转换成字节文件 Stream strm = request.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength);//读取文件2048个字节,偏移为0,并赋值给buff字节组; while (contentLen != 0) { strm.Write(buff, 0, contentLen); //重点方法,将流写入 contentLen = fs.Read(buff, 0, buffLength); } fs.Close(); strm.Close(); } /// <summary> /// FTP下载 注意设置FTP读写权限 /// </summary> /// <param name="FTPFilePath">要下载的FTP文件的路径</param> /// <param name="LoadPath">本地文件夹的路径</param> public static void _____DownloadFile(string FTPFilePath, string LocalPath, string userName, string password) { FtpWebRequest request = CreateFtpWebRequest(FTPFilePath, userName, password, WebRequestMethods.Ftp.DownloadFile); FtpWebResponse response = (FtpWebResponse)request.GetResponse();//获得相应体 FileStream filestream = File.Create(LocalPath + Path.GetFileName(FTPFilePath)); Stream responseStream = response.GetResponseStream(); int buflength = 2048; byte[] buffer = new byte[buflength]; int bytesRead; bytesRead = responseStream.Read(buffer, 0, buflength); while (bytesRead != 0) { filestream.Write(buffer, 0, bytesRead); bytesRead = responseStream.Read(buffer, 0, buflength); } responseStream.Close(); filestream.Close(); } //创建FTP请求 private static FtpWebRequest CreateFtpWebRequest(string uri, string UserName, string Password, string requestMethod) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); request.Credentials = new NetworkCredential(UserName, Password); request.KeepAlive = true;//请求完成后是否保持活动状态,不断开连接 request.UseBinary = true;//指示服务器要传输的二进制数据 request.UsePassive = true;//被动 request.Method = requestMethod; return request; } }
这篇关于【C#随手记】FTP上传与下载的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具