using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;namespace EuSoft.Common
{public class FTPHelper{#region FTP获取文件列表/// <summary>/// FTP获取文件列表/// </summary>/// <param name="ftpServerIP"></param>/// <param name="ftpUserID"></param>/// <param name="ftpPassword"></param>/// <returns></returns>public string[] FTPGetFileList(string ftpServerIP, string ftpUserID, string ftpPassword){//响应结果StringBuilder result = new StringBuilder();//FTP请求FtpWebRequest ftpRequest = null;//FTP响应WebResponse ftpResponse = null;//FTP响应流StreamReader ftpResponsStream = null;try{//生成FTP请求ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));//设置文件传输类型ftpRequest.UseBinary = true;//FTP登录ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);//设置FTP方法ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;//生成FTP响应ftpResponse = ftpRequest.GetResponse();//FTP响应流ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream());string line = ftpResponsStream.ReadLine();while (line != null){result.Append(line);result.Append("\n");line = ftpResponsStream.ReadLine();}//去掉结果列表中最后一个换行result.Remove(result.ToString().LastIndexOf('\n'), 1);//返回结果return result.ToString().Split('\n');}catch (Exception ex){Console.WriteLine(ex.Message);return (null);}finally{if (ftpResponsStream != null){ftpResponsStream.Close();}if (ftpResponse != null){ftpResponse.Close();}}}#endregion#region FTP修改文件public void FTPUpdateFile(string ftpServerIP, string ftpUserID, string ftpPassword, string filename,string newfilename){//定义FTP请求对象FtpWebRequest ftpRequest = null;//定义FTP响应对象FtpWebResponse ftpResponse = null;//FTP数据流Stream ftpStream = null;try{ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+ftpServerIP +"/"+ filename));ftpRequest.Method = WebRequestMethods.Ftp.Rename;ftpRequest.RenameTo = newfilename;ftpRequest.UseBinary = true;ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();ftpStream = response.GetResponseStream();ftpStream.Close();response.Close();}catch (Exception ex){Console.WriteLine(ex.Message);}finally{if (ftpStream != null){ftpStream.Close();}if (ftpResponse != null){ftpResponse.Close();}}}#endregion#region FTP下载文件/// <summary>/// FTP下载文件/// </summary>/// <param name="ftpServerIP">FTP服务器IP</param>/// <param name="ftpUserID">FTP登录帐号</param>/// <param name="ftpPassword">FTP登录密码</param>/// <param name="saveFilePath">保存文件路径</param>/// <param name="saveFileName">保存文件名</param>/// <param name="downloadFileName">下载文件名</param>public void FTPDownloadFile(string ftpServerIP, string ftpUserID, string ftpPassword,string saveFilePath, string saveFileName, string downloadFileName){//定义FTP请求对象FtpWebRequest ftpRequest = null;//定义FTP响应对象FtpWebResponse ftpResponse = null;//存储流FileStream saveStream = null;//FTP数据流Stream ftpStream = null;try{//生成下载文件saveStream = new FileStream(saveFilePath + "\\" + saveFileName, FileMode.Create);//生成FTP请求对象ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + downloadFileName));//设置下载文件方法ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;//设置文件传输类型ftpRequest.UseBinary = true;//设置登录FTP帐号和密码ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);//生成FTP响应对象ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();//获取FTP响应流对象ftpStream = ftpResponse.GetResponseStream();//响应数据长度long cl = ftpResponse.ContentLength;int bufferSize = 2048;int readCount;byte[] buffer = new byte[bufferSize];//接收FTP文件流readCount = ftpStream.Read(buffer, 0, bufferSize);while (readCount > 0){saveStream.Write(buffer, 0, readCount);readCount = ftpStream.Read(buffer, 0, bufferSize);}}catch (Exception ex){Console.WriteLine(ex.Message);}finally{if (ftpStream != null){ftpStream.Close();}if (saveStream != null){saveStream.Close();}if (ftpResponse != null){ftpResponse.Close();}}}#endregion#region FTP上传文件/// <summary>/// FTP上传文件/// </summary>/// <param name="ftpServerIP">FTP服务器IP</param>/// <param name="ftpUserID">FTP登录帐号</param>/// <param name="ftpPassword">FTP登录密码</param>/// <param name="filename">上文件文件名(绝对路径)</param>public void FTPUploadFile(string ftpServerIP, string ftpUserID, string ftpPassword, string filename){//上传文件FileInfo uploadFile = null;//上传文件流FileStream uploadFileStream = null;//FTP请求对象FtpWebRequest ftpRequest = null;//FTP流Stream ftpStream = null;try{//获取上传文件uploadFile = new FileInfo(filename);//创建FtpWebRequest对象ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + uploadFile.Name));//FTP登录ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// 默认为true,连接不会被关闭// 在一个命令之后被执行ftpRequest.KeepAlive = false;//FTP请求执行方法ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;// 指定数据传输类型ftpRequest.UseBinary = true;// 上传文件时通知服务器文件的大小ftpRequest.ContentLength = uploadFile.Length;// 缓冲大小设置为2kbint buffLength = 2048;byte[] buff = new byte[buffLength];int contentLen;// 打开一个文件流读上传的文件uploadFileStream = uploadFile.OpenRead();// 把上传的文件写入流ftpStream = ftpRequest.GetRequestStream();// 每次读文件流的2kbcontentLen = uploadFileStream.Read(buff, 0, buffLength);// 流内容没有结束while (contentLen != 0){// 把内容从file stream 写入 upload streamftpStream.Write(buff, 0, contentLen);contentLen = uploadFileStream.Read(buff, 0, buffLength);}}catch (Exception ex){Console.WriteLine(ex.Message);}finally{if (uploadFileStream != null){uploadFileStream.Close();}if (ftpStream != null){ftpStream.Close();}}}#endregion}
}

C#编程, FTP文件上传、下载、重命名公共类相关推荐

  1. java ftp 下载慢_Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题

    废话不多说了,直接给大家贴代码了,具体代码如下所示: //文件上传 public static boolean uploadToFTP(String url,int port,String usern ...

  2. C++:FTP文件上传下载(附完整源码)

    C++:FTP文件上传下载 FTP文件下载 FTP文件上传 FTP文件下载 #include #include #include #pragma comment(lib, "WinInet. ...

  3. java ftp下载文件 慢_java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题...

    标签: //文件上传 public static boolean uploadToFTP(String url,int port,String username,String password,Str ...

  4. java ftp 中文上传_java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题...

    //文件上传 public static boolean uploadToFTP(String url,int port,String username,String password,String ...

  5. Transmit 5.5.1 for Mac 中文共享版 – 优秀的FTP文件上传下载客户端

    下载地址:风云社区 macOS文件传输应用程序的黄金标准刚刚推向未来.Transmit 5.5.1 在这里.使用简单,熟悉且功能强大的UI上传,下载和管理大量服务器上的文件. 更多服务器: Trans ...

  6. Android局域网实现FTP文件上传下载客户端与服务端

    文章目录 前言 一.FTP是什么? 二.使用步骤 1 服务端 1.1 服务端的代码实现 2 客户端 2.1 客户端的代码实现 附件 前言 最近在公司的项目中,使用到了 局域网通信,不同的设备直接传递消 ...

  7. ftp文件上传下载等系列操作

    一.安装tfp ubuntu系统: apt-get install ftp 二.ftp连接 访问ftp server: ftp 192.168.120.xxx 输入用户名密码即可 admin/xxxx ...

  8. .Net FTP文件上传下载及相关文件操作

    public class FtpHelper{string ftpServerIP;string ftpRemotePath;string ftpUserID;string ftpPassword;s ...

  9. [C# 网络编程系列]专题十一:实现一个基于FTP协议的程序——文件上传下载器...

    引言: 在这个专题将为大家揭开下FTP这个协议的面纱,其实学习知识和生活中的例子都是很相通的,就拿这个专题来说,要了解FTP协议然后根据FTP协议实现一个文件下载器,就和和追MM是差不多的过程的,相信 ...

  10. FTP编程实验——实现文件上传下载(基于Python3.7和PyQt5)

    目录 FTP编程实现文件上传下载(基于Python3.7和PyQt5) 一.实验目的 二.实验内容 三.实验步骤 (一)服务器端 (二)客户端 [1] 界面设计 [2] 生成布局代码 [3] 功能实现 ...

最新文章

  1. R语言使用ggplot2包使用geom_boxplot函数绘制基础分组箱图(分组箱体框颜色配置)实战
  2. jquery的attr和prop区别之实例
  3. myeclipse定位代码文件位置
  4. python代码教程-(Python基础教程之三)Python代码中添加注释
  5. OpenCV神经网络ANN代码编译运行与解读(二)
  6. VC++把输入的字符转换为十六进制
  7. 必看2021年80后夫妻同时过信息系统项目管理师
  8. java插入排序实现,经典(Java版)排序算法的分析及实现之一直接插入排序
  9. 蓝桥杯 ADV-156算法提高 分分钟的碎碎念(动态规划)
  10. iOS底层探索之多线程(六)—GCD源码分析(sync 同步函数、async 异步函数)
  11. 2022年深圳数据分析师推荐考这个证书-CPDA
  12. Exception 异常和自定义异常
  13. 《老梁四大名著情商课》笔记- 刚上班,别做林黛玉,也别做孙悟空
  14. JS---------------网页版的消灭星星
  15. OutLook Express关联QQ邮箱
  16. Unity3D 无限滚动列表
  17. Linux学习笔记之软件安装
  18. 游戏开发之棋牌游戏的未来
  19. java爬虫爬取音乐
  20. 视图、存储过程、触发器讲解

热门文章

  1. 验证中文和英文姓名 正则表达式
  2. 【2016-10-14收获】Cloud IDEs
  3. iOS科普一下根View及其子View中心点含义的坑
  4. word2010添加论文引用
  5. Quartz2D学习记录
  6. 博客文章内容导航(实时更新)
  7. 网站备案后 换服务器,网站备案后更换服务器
  8. 快排两种实现及五种优化
  9. 如何查看DNS记录的生存时间(TTL)?
  10. excel数据透视表_Excel数据透视表可轻松实现总计