• .NET ftp文件上传和下载


    文章参考来源地址:https://blog.csdn.net/wybshyy/article/details/52095542

    本次对代码进行了一点扩展:将文件上传到ftp指定目录下,若目录不存在则创建目录。

    调整点:在构造函数中,处理和获得可直接使用的ftpURI。

    完整代码如下:

    public class FtpWeb
        {
            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 FtpWeb(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
            {
                ftpServerIP = FtpServerIP;
                ftpRemotePath = FtpRemotePath;
                ftpUserID = FtpUserID;
                ftpPassword = FtpPassword;
                //ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
                ftpURI = CheckDirectory();
            }
    
            /// <summary>
            /// 上传
            /// </summary>
            /// <param name="filename"></param>
            public bool Upload(string filename, out string errorMsg)
            {
                errorMsg = "";
                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; //开辟2KB缓存区
                byte[] buff = new byte[buffLength];
                int contentLen;
                FileStream fs = fileInf.OpenRead();
                try
                {
                    Stream strm = reqFTP.GetRequestStream();
                    contentLen = fs.Read(buff, 0, buffLength);
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                    }
                    strm.Close();
                    fs.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    errorMsg = ex.Message;
                    return false;
                }
            }
    
            /// <summary>
            /// 下载
            /// </summary>
            /// <param name="filePath"></param>
            /// <param name="fileName"></param>
            public bool Download(string filePath, string fileName, out string errorMsg)
            {
                errorMsg = "";
                FtpWebRequest reqFTP;
                try
                {
                    FileStream outputStream = new FileStream(filePath + "//" + fileName, FileMode.Create);
    
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    reqFTP.UseBinary = true;
                    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 true;
                }
                catch (Exception ex)
                {
                    errorMsg = ex.Message;
                    return false;
                }
            }
            #region 私有
            /// <summary>
            /// 获得+建立文件所在物理路径
            /// </summary>
            /// <returns></returns>
            private string CheckDirectory()
            {
                string dir = "ftp://" + ftpServerIP + "/";//
                if (!string.IsNullOrWhiteSpace(ftpRemotePath))
                {
                    var folderArr = ftpRemotePath.Split(new string[] { @"", @"/" }, StringSplitOptions.RemoveEmptyEntries);
                    if (folderArr != null && folderArr.Length > 0)
                    {
                        foreach (var folder in folderArr)
                        {
                            dir = dir + folder + "/";
                            MakeDir(dir);
                        }
                    }
                }
                return dir;
            }
            /// <summary>
            /// 创建目录
            /// </summary>
            /// <param name="errorMsg"></param>
            /// <returns></returns>
            private string MakeDir(string dir)
            {
                string errorMsg = "";
                FtpWebRequest reqFTP;
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(dir));
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                    reqFTP.KeepAlive = false;
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    response.Close();
                }
                catch (Exception ex)
                {
                    errorMsg = "创建文件失败,原因: " + ex.Message;
                }
                return errorMsg;
            }
            #endregion
        }
  • 相关阅读:
    在安卓上用Termux安装sqlmap
    地址转换函数
    字节操作函数
    主机字节序和网络字节序
    20191231 Spring官方文档(Core 1.13-1.14)
    20191230 Spring官方文档(Core 1.12)
    20191230 Tomcat权威指南-读书摘要系列【归档】
    20191230 On Java8【归档】
    20191227 Java8 日期时间API
    20191226 Spring官方文档(Core 1.11)
  • 原文地址:https://www.cnblogs.com/senyier/p/11161805.html
Copyright © 2020-2023  润新知