[C#]工具类—FTP上传下载

简介: public class FtpHelper { /// /// ftp方式上传 /// public static int UploadFtp(string filePath, string filena...
public class FtpHelper
    { 
        /// <summary>
        /// ftp方式上传 
        /// </summary>
        public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
            string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
            FtpWebRequest reqFTP;
            // Create FtpWebRequest object from the Uri provided 
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
            try
            {
                // Provide the WebPermission Credintials 
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
 
                // By default KeepAlive is true, where the control connection is not closed 
                // after a command is executed. 
                reqFTP.KeepAlive = false;
 
                // Specify the command to be executed. 
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
 
                // Specify the data transfer type. 
                reqFTP.UseBinary = true;
 
                // Notify the server about the size of the uploaded file 
                reqFTP.ContentLength = fileInf.Length;
 
                // The buffer size is set to 2kb 
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;
 
                // Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
                //FileStream fs = fileInf.OpenRead(); 
                FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
 
                // Stream to which the file to be upload is written 
                Stream strm = reqFTP.GetRequestStream();
 
                // Read from the file stream 2kb at a time 
                contentLen = fs.Read(buff, 0, buffLength);
 
                // Till Stream content ends 
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream 
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
 
                // Close the file stream and the Request Stream 
                strm.Close();
                fs.Close();
                return 0;
            }
            catch (Exception ex)
            {
                reqFTP.Abort();
                //  Logging.WriteError(ex.Message + ex.StackTrace);
                return -2;
            }
        }
 
        /// <summary>
        /// ftp方式下载 
        /// </summary>
        public static int DownloadFtp(string filePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            FtpWebRequest reqFTP;
            try
            {
                //filePath = < <The full path where the file is to be created.>>, 
                //fileName = < <Name of the file to be created(Need not be the name of the file on FTP server).>> 
                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
 
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = false;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.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);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
 
                ftpStream.Close();
                outputStream.Close();
                response.Close();
                return 0;
            }
            catch (Exception ex)
            {
                // Logging.WriteError(ex.Message + ex.StackTrace);
                // System.Windows.Forms.MessageBox.Show(ex.Message);
                return -2;
            }
        }
    }

  不错的文章:http://www.cnblogs.com/greatverve/archive/2012/03/03/csharp-ftp.html

目录
相关文章
|
网络协议 C# 文件存储
C# 利用FluentFTP实现FTP上传下载功能
C# 利用FluentFTP实现FTP上传下载功能
473 0
C# 利用FluentFTP实现FTP上传下载功能
|
C# 对象存储
C#上传阿里云OSS工具类AliOSSTool
C#上传阿里云OSS工具类AliOSSTool
398 0
|
3月前
|
数据采集 JavaScript C#
C#图像爬虫实战:从Walmart网站下载图片
C#图像爬虫实战:从Walmart网站下载图片
|
4月前
|
数据采集 XML JavaScript
C# 中 ScrapySharp 的多线程下载策略
C# 中 ScrapySharp 的多线程下载策略
|
2月前
|
监控 前端开发 安全
C#一分钟浅谈:文件上传与下载功能实现
【10月更文挑战第2天】在Web应用开发中,文件的上传与下载是常见需求。本文从基础入手,详细讲解如何在C#环境下实现文件上传与下载。首先介绍前端表单设计及后端接收保存方法,使用`&lt;input type=&quot;file&quot;&gt;`与`IFormFile`接口;接着探讨错误处理与优化策略,如安全性验证和路径管理;最后讲解文件下载的基本步骤,包括确定文件位置、设置响应头及发送文件流。此外,还提供了进阶技巧,如并发处理、大文件分块上传及进度监控,帮助开发者构建更健壮的应用系统。
149 15
|
4月前
|
Java
Java SpringBoot FTP 上传下载文件
Java SpringBoot FTP 上传下载文件
164 0
|
6月前
|
关系型数据库 C# 数据库
技术笔记:MSCL超级工具类(C#),开发人员必备,开发利器
技术笔记:MSCL超级工具类(C#),开发人员必备,开发利器
57 3
|
7月前
|
数据采集 API C#
网页解析高手:C#和HtmlAgilityPack教你下载视频
使用C#和HtmlAgilityPack解析小红书网页,下载其视频内容。文章涵盖了解析网页、获取视频链接、C#实现、HtmlAgilityPack简化解析、代理IP确保下载稳定及多线程提高下载效率。提供的代码示例展示了如何设置代理和多线程下载视频。实验结果显示,该方法能有效、高效地下载小红书视频。
139 5
网页解析高手:C#和HtmlAgilityPack教你下载视频
|
C# 开发者
C#扩展方法和工具类的区别
扩展方法和工具类的主要区别在于它们的作用。扩展方法旨在扩展现有的类,而工具类旨在提供一组通用且可靠的方法来执行某些任务。当需要扩展现有的类时,使用扩展方法;当需要实用函数来执行通用任务时,使用工具类。
85 0
|
7月前
|
JSON 网络协议 C#
C# 工具类
C# 工具类
50 1