FTP文件上传下载(C#)

摘要:
以下是ftp上传和下载工具,无法直接运行。请删除不必要的代码。

下面是ftp上传下载工具,不能直接运行,请删除不必要的代码。

/// <summary>
    /// ftp文件上传下载
    /// </summary>
    public class FtpHelper
    {
        private string FtpServer;
        private string FtpDefaultUrl;
        private string LogginID;
        private string LoginPWD;
        private string DownloadPath;

        private FtpHelper() { }

        public FtpHelper(string ftpServer, string ftpDefaultUrl, string loginID, string loginPWD, string downloadPath)
        {
            this.FtpServer = ftpServer;
            this.FtpDefaultUrl = ftpDefaultUrl;
            this.LogginID = loginID;
            this.LoginPWD = loginPWD;
            this.DownloadPath = downloadPath;
        }


        public bool DownloadFile(string fileName)
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(string.Format("{0}/{1}", FtpServer+FtpDefaultUrl, fileName));
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.Credentials = new NetworkCredential(LogginID, LoginPWD);

            try
            {
                using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
                {
                    string localfile = Path.Combine(DownloadPath, fileName);
                    FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);
                    int buffer = 1024;
                    byte[] b = new byte[buffer];
                    int i = 0;
                    Stream stream = ftpResponse.GetResponseStream();
                    while ((i = stream.Read(b, 0, buffer)) > 0)
                    {
                        fs.Write(b, 0, i);
                    }

                    stream.Flush();
                    fs.Flush();
                    stream.Close();
                    fs.Close();
                }
                Log.WriteLog(fileName + "文件下载成功");
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("ftp下载文件出错:");
                Log.WriteLog(ex);

                return false;
            }
        }


        public bool UploadFile(string localFile, string ftpRemotePath)
        {
            FileInfo file = new FileInfo(localFile);
            FileStream fileStream = file.OpenRead();
            long length = fileStream.Length;
            string url = "";
            if (string.IsNullOrEmpty(ftpRemotePath))
            {
                url = string.Format("{0}/{1}", FtpServer+FtpDefaultUrl, file.Name);
            }
            else
            {
                url = string.Format("{0}/{1}/{2}", FtpServer+FtpDefaultUrl, ftpRemotePath, file.Name);
            }
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(url);
            ftpRequest.Credentials = new NetworkCredential(this.LogginID, this.LoginPWD);
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.UseBinary = true;
            ftpRequest.ContentLength = length;
            ftpRequest.Timeout = 10 * 1000;

            try
            {
                Stream stream = ftpRequest.GetRequestStream();
                int bufferLength = 2048;
                byte[] b = new byte[bufferLength];

                int i;
                while ((i = fileStream.Read(b, 0, bufferLength)) > 0)
                {
                    stream.Write(b, 0, i);
                }
                stream.Close();
                stream.Dispose();

                return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("ftp上传文件失败:");
                Log.WriteLog(ex);
                return false;
            }
        }
    }

免责声明:文章转载自《FTP文件上传下载(C#)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇为应用程序池 ''DefaultAppPool'' 提供服务的进程意外终止。进程 ID 是 ''xxx''问题的解决方法重新安装 tcp/ip协议下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PHP连接FTP服务的简单实现

PHP连接FTP服务: <?php class Ftp { private $connect; private $getback; /** * ftp连接信息 * @var array */ private $ftpConnInfo = [ 'h...

JAVA 比较两张图片的相似度

原文:http://liuguihua0823.iteye.com/blog/1178118#bc2395917 import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; /** * 比较两张图片的相似度...

golang-指针,函数,map

指针普通类型变量存的就是值,也叫值类型。指针类型存的是地址,即指针的值是一个变量的地址。一个指针只是值所保存的位置,不是所有的值都有地址,但是所有的变量都有。使用指针可以在无需知道变量名字的情况下,间接读取或更新变量的值。 获取变量的地址,用&,例如:var a int 获取a的地址:&a,&a(a的地址)这个表达式获取一个指向整型...

使用spring-rabbit测试RabbitMQ消息确认(发送确认,接收确认)

1、首先是rabbitmq的配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...

使用CefSharp跳转页面不弹出页面:

using CefSharp; using CefSharp.Wpf; namespace Common.Control { internal class CefSharpOpenPageSelf : ILifeSpanHandler { public bool DoClose(IWebBrowser browserCont...

对象Bean与Map互转问题

一、摘要 在实际开发过程中,经常碰到需要进行对象与map之间互转的问题,其实对于对象、Map 之间进行互转有很多种方式,下面我们一起来梳理一下: 利用 JSON 工具包,将对象转成字符串,之后再转成 Map,这种需要转换2次,相对来说效率比较底; 利用 Java 反射,获取 Bean 类的属性和值,再转换到 Map 对应的键值对中,相对来说这种方法效率高...