博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSIS 学习之旅 FTP访问类
阅读量:4307 次
发布时间:2019-06-06

本文共 10050 字,大约阅读时间需要 33 分钟。

这章把脚本任务访问FTP的方法 全部给大家。

控件的使用大家如果有不懂得可以看下我之前的文章。

第一章:

第二章:

第三章:

第四章:

第五章:

 

#region 连接FTP服务器        ///           /// 连接FTP服务器        ///           /// FTP连接地址          /// 指定FTP连接成功后的当前目录, 如果不指定即默认为根目录          public string FTPHelper(string FtpServerIP, string FtpRemotePath)        {            string ftpURI = "ftp://" + FtpServerIP + "/" + FtpRemotePath + "/";            return ftpURI;        }        #endregion        #region 文件上传FTP服务器        ///         /// 上传        ///         /// 文件详细路径        /// FTPUrl        /// 用户名        /// 密码        public void Upload(string FilePathPendingAndName, string FTPUrl, string FTP_UserName, string FTP_PWD)        {            FileInfo fileInf = new FileInfo(FilePathPendingAndName);            FtpWebRequest reqFTP;            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPUrl + fileInf.Name));            reqFTP.Credentials = new NetworkCredential(FTP_UserName, FTP_PWD);            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;            reqFTP.KeepAlive = false;            reqFTP.UseBinary = true;            reqFTP.ContentLength = fileInf.Length;            int buffLength = 2048;            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();            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }      #endregion                #region 下载文件        ///         /// 下载文件        ///         /// 本地路径        /// 文件名        /// FTP链接路径        /// 用户名        /// 密码        public void Download(string filePath, string fileName, string ftpUrl, string FTP_UserName, string FTP_PWD)        {            try            {                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);                FtpWebRequest reqFTP;                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUrl + fileName));                reqFTP.Credentials = new NetworkCredential(FTP_UserName, FTP_PWD);                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;                reqFTP.UseBinary = true;                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();            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }        #endregion     #region 删除文件        ///          /// 删除文件         ///          public void Delete(string fileName)        {            try            {                FtpWebRequest reqFTP;                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;                reqFTP.KeepAlive = false;                string result = String.Empty;                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();                long size = response.ContentLength;                Stream datastream = response.GetResponseStream();                StreamReader sr = new StreamReader(datastream);                result = sr.ReadToEnd();                sr.Close();                datastream.Close();                response.Close();            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }     #endregion    #region 获取当前目录下文件列表(不包括文件夹)        ///         /// 获取当前目录下文件列表(不包括文件夹)        ///         /// 连接FTP服务器地址        /// 用户名        /// 密码        /// 
public string[] GetFileList(string url, string ftpUserName, string ftpPassword) { StringBuilder result = new StringBuilder(); FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); string FileName = ""; while (line != null) { if (line.IndexOf("
") == -1) { FileName = ""; FileName = Regex.Match(line, @"(?<=IN)([.\S\s]*)(?=csv)", RegexOptions.IgnoreCase).Value.ToString() ; if (FileName.Trim() != "") { FileName = "IN" + FileName + "csv"; result.Append(FileName + "|"); } } line = reader.ReadLine(); } //result.Remove(result.ToString().LastIndexOf('\n'), 1); reader.Close(); response.Close(); } catch (Exception ex) { throw (ex); } return result.ToString().Split('|'); } #endregion #region 判断当前目录下指定的文件是否存在 ///
/// 判断当前目录下指定的文件是否存在 /// ///
远程文件名 public bool FileExist(string FTPUrl, string RemoteFileName, string FTP_UserName, string FTP_PWD) { string FileName = "IN_NORMAL_" + Regex.Match(RemoteFileName, @"(?<=IN_NORMAL_)([.\S\s]*)(?=csv)", RegexOptions.IgnoreCase).Value.ToString() + "csv"; string[] fileList = GetFileList(FTPUrl, FTP_UserName, FTP_PWD); foreach (string str in fileList) { if (str.Trim()==FileName.Trim()) { return true; } } return false; } #endregion #region 更改文件名 ///
/// 更改文件名 /// ///
现有文件名称 ///
新的文件名称 ///
FTPUrl ///
用户名 ///
密码 public void ReName(string currentFilename, string newFilename, string FTPUrl, string FTP_UserName, string FTP_PWD) { FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPUrl + currentFilename)); reqFTP.Method = WebRequestMethods.Ftp.Rename; reqFTP.RenameTo = newFilename; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(FTP_UserName, FTP_PWD); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); //File.Move() ftpStream.Close(); response.Close(); } catch (Exception ex) { } } #endregion #region 移动文件夹 ///
/// 移动文件夹 /// ///
现有文件名称 ///
新的文件名称 ///
FTPUrl ///
用户名 ///
密码 public void MovieFile(string currentFilename, string newDirectory,string FTPUrl, string FTP_UserName, string FTP_PWD) { ReName(currentFilename, newDirectory, FTPUrl, FTP_UserName, FTP_PWD); } #endregion #region 创建文件夹 ///
/// 创建文件夹 /// public void MakeDir(string dirName) { FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName)); reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); ftpStream.Close(); response.Close(); } catch (Exception ex) { } } #endregion #region 获取指定文件大小 ///
/// 获取指定文件大小 /// public long GetFileSize(string filename) { FtpWebRequest reqFTP; long fileSize = 0; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename)); reqFTP.Method = WebRequestMethods.Ftp.GetFileSize; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); fileSize = response.ContentLength; ftpStream.Close(); response.Close(); } catch (Exception ex) { } return fileSize; } #endregion

 

至此 SSIS 学习之旅 到这里就结束了。希望对大家的工作有所帮助吧。

 

转载于:https://www.cnblogs.com/yinsq/p/5482810.html

你可能感兴趣的文章
谷歌浏览器离线安装包下载
查看>>
正则表达式
查看>>
AWK命令使用
查看>>
Redis项目实战---应用及理论(三)---Jedis使用
查看>>
Redis项目实战--应用及理论(一)--redis基础
查看>>
Redis项目实战---应用及理论(二)---Redis集群原理
查看>>
VMware vSphere API开发(一)---vSphere 体系核心概念
查看>>
java String 的比较
查看>>
将String数字字符转为整型
查看>>
【转】 Java中equals和==的区别
查看>>
idea导入maven项目时需要注意
查看>>
nginx部署前端项目的一些配置【刚入门】
查看>>
java 日期格式化 将String日期重新格式化成String型【转】
查看>>
Linux下python安装升级详细步骤 | Python2 升级 Python3
查看>>
阿里云CentOS安装图形化界面
查看>>
SpringBoot nohup启动
查看>>
PHP pclzip.php 解压中文乱码
查看>>
Jenkins安装 maven插件
查看>>
数学好玩 沛沛猜想
查看>>
银联号
查看>>