• C# FTP上传与下载文件


    public class UploadFile
        {
            string ftpServerIP;
            string ftpRemotePath;
            string ftpUserID;
            string ftpPassword;
            string ftpURI;
            /// <summary>
            /// 连接FTP
            /// </summary>
            /// <param name="FtpServerIP">FTP连接地址</param>
            /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
            /// <param name="FtpUserID">用户名</param>
            /// <param name="FtpPassword">密码</param>
            public UploadFile(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
            {
                ftpServerIP = FtpServerIP;
                ftpRemotePath = FtpRemotePath;
                ftpUserID = FtpUserID;
                ftpPassword = FtpPassword;
                ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
            }
            /// <summary>
            /// 上传
            /// </summary>
            /// <param name="filename"></param>
            public void Upload(string filename)
            {
                log4net.ILog log = log4net.LogManager.GetLogger(typeof(UploadFile));
                FileInfo fileInf = new FileInfo(filename);
                string uri = ftpURI + fileInf.Name;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.UseBinary = true;
                reqFTP.ContentLength = fileInf.Length;
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;
                try
                {
                    using (FileStream fs = fileInf.OpenRead())
                    {
                        using (Stream strm = reqFTP.GetRequestStream())
                        {
                            contentLen = fs.Read(buff, 0, buffLength);
                            while (contentLen != 0)
                            {
                                strm.Write(buff, 0, contentLen);
                                contentLen = fs.Read(buff, 0, buffLength);
                            }
                        }
                        if (log.IsErrorEnabled)
                        {
                            log.Error(string.Format("
    ----------- {0} ----------
    ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                            log.Error(fileInf.FullName + "上传到" + ftpURI + "成功");
                            log.Error("
    ");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    public class DownLoadFile
        {
            string ftpServerIP;
            string ftpRemotePath;
            string ftpUserID;
            string ftpPassword;
            string fileName;
            string ftpURI;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="FtpServerIP">FTP连接地址</param>
            /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
            /// <param name="FtpUserID">用户名</param>
            /// <param name="FtpPassword">密码</param>
            /// <param name="fileName">文件名</param>
            public DownLoadFile(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword,string FileName)
            {
                ftpServerIP = FtpServerIP;
                ftpRemotePath = FtpRemotePath;
                ftpUserID = FtpUserID;
                ftpPassword = FtpPassword;
                fileName = FileName;
                ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/"+fileName;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="filepath">本地存放目录</param>
            public void DownLoadFileFromFTP(string filepath) {
                FtpWebRequest ftp =(FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
                ftp.Credentials = new NetworkCredential(ftpUserID,ftpPassword);
                ftp.KeepAlive = false;
                ftp.Method = WebRequestMethods.Ftp.DownloadFile;
                ftp.UseBinary = true;
                ftp.UsePassive = false;
                using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (FileStream fs = new FileStream(filepath+"\"+fileName, FileMode.CreateNew))
                        {
                            try
                            {
                                long cl = response.ContentLength;
                                int bufferSize = 2048;
                                int readCount;
                                byte[] buffer = new byte[bufferSize];
                                readCount = responseStream.Read(buffer, 0, bufferSize);
                                while (readCount > 0)
                                {
                                    fs.Write(buffer, 0, readCount);
                                    readCount = responseStream.Read(buffer, 0, bufferSize);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }
                }
            }
        }

    在main方法中进行调用示例

    static void Main(string[] args)
            {
                UploadFile uploadFile = new UploadFile("172.18.14.67:9001", "", "administrator", "123");
                uploadFile.Upload("InsuranceInfo/XFRS106020170822.txt");
                DownLoadFile downloadFile = new DownLoadFile("172.18.14.67:9001", "", "administrator", "123", "XFRS106020170821.txt");
                downloadFile.DownLoadFileFromFTP("D:\");
            }
  • 相关阅读:
    java基础 Collections.sort的两种用法
    Mysql常用命令详解
    2、Java并发编程:如何创建线程
    JAR、WAR、EAR的使用和区别
    区分Oracle的数据库,实例,服务名,SID
    Mysql 启动运行
    3、Java并发编程:Thread类的使用
    1、Java多线程基础:进程和线程之由来
    文件上传利器SWFUpload使用指南
    网络矩阵
  • 原文地址:https://www.cnblogs.com/z-huan/p/7411472.html
Copyright © 2020-2023  润新知