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

控件的使用大家如果有不懂得可以看下我之前的文章。
第一章:SSIS 学习之旅 第一个SSIS 示例(一)(上)

第二章:SSIS 学习之旅 第一个SSIS 示例(二)

第三章:SSIS 学习之旅 数据同步

第四章:SSIS 学习之旅 FTP文件传输-FTP任务

第五章:SSIS 学习之旅 FTP文件传输-脚本任务

        #region 连接FTP服务器
/// <summary>
/// 连接FTP服务器
/// </summary>
/// <param name="FtpServerIP">FTP连接地址</param>
/// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
public string FTPHelper(string FtpServerIP, string FtpRemotePath)
{
string ftpURI = "ftp://" + FtpServerIP + "/" + FtpRemotePath + "/";
return ftpURI;
} #endregion #region 文件上传FTP服务器
/// <summary>
/// 上传
/// </summary>
/// <param name="FilePathPendingAndName">文件详细路径</param>
/// <param name="FTPUrl">FTPUrl</param>
/// <param name="FTP_UserName">用户名</param>
/// <param name="FTP_PWD">密码</param>
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 = ;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, , buffLength);
while (contentLen != )
{
strm.Write(buff, , contentLen);
contentLen = fs.Read(buff, , buffLength);
}
strm.Close();
fs.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} #endregion #region 下载文件
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath">本地路径</param>
/// <param name="fileName">文件名</param>
/// <param name="ftpUrl">FTP链接路径</param>
/// <param name="FTP_UserName">用户名</param>
/// <param name="FTP_PWD">密码</param>
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 = ;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, , bufferSize);
while (readCount > )
{
outputStream.Write(buffer, , readCount);
readCount = ftpStream.Read(buffer, , bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion #region 删除文件
/// <summary>
/// 删除文件
/// </summary>
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 获取当前目录下文件列表(不包括文件夹)
/// <summary>
/// 获取当前目录下文件列表(不包括文件夹)
/// </summary>
/// <param name="url">连接FTP服务器地址</param>
/// <param name="ftpUserName">用户名</param>
/// <param name="ftpPassword">密码</param>
/// <returns></returns>
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("<DIR>") == -)
{
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 判断当前目录下指定的文件是否存在
/// <summary>
/// 判断当前目录下指定的文件是否存在
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
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 更改文件名
/// <summary>
/// 更改文件名
/// </summary>
/// <param name="currentFilename">现有文件名称</param>
/// <param name="newDirectory">新的文件名称</param>
/// <param name="FTPUrl">FTPUrl</param>
/// <param name="FTP_UserName">用户名</param>
/// <param name="FTP_PWD">密码</param>
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 移动文件夹
/// <summary>
/// 移动文件夹
/// </summary>
/// <param name="currentFilename">现有文件名称</param>
/// <param name="newDirectory">新的文件名称</param>
/// <param name="FTPUrl">FTPUrl</param>
/// <param name="FTP_UserName">用户名</param>
/// <param name="FTP_PWD">密码</param>
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 创建文件夹
/// <summary>
/// 创建文件夹
/// </summary>
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 获取指定文件大小
/// <summary>
/// 获取指定文件大小
/// </summary>
public long GetFileSize(string filename)
{
FtpWebRequest reqFTP;
long fileSize = ;
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 学习之旅 到这里就结束了。希望对大家的工作有所帮助吧。

SSIS 学习之旅 FTP访问类的更多相关文章

  1. SSIS 学习之旅 FTP文件传输-脚本任务

    这一章主要讲解一下用脚本怎么把CSV文件抛送到FTP服务器上 设计:   通过Demon库的Users表数据生成CSV文件.   生成后的CSV文件抛送到FTP指定目录下. 控件的使用这里就不做详细讲 ...

  2. SSIS 学习之旅 FTP文件传输-FTP任务

    这一章主要讲解一下FTP控件. 设计:   通过Demon库的Users表数据生成CSV文件.   生成后的CSV文件抛送到FTP指定目录下. 其他控件的使用这里就不做详细讲解了.大家如果有不懂得可以 ...

  3. SSIS 学习之旅 序章 和 简介

    SSIS 学习之旅目录: 第一章: SSIS 学习之旅 第一个SSIS 示例(一) 第二章: SSIS 学习之旅 第一个SSIS 示例(二) 第三章: SSIS 学习之旅 数据同步 第四章: SSIS ...

  4. C#自定义FTP访问类的代码

    如下资料是关于C#自定义FTP访问类的代码,应该对各朋友有帮助. using System; using System.Collections.Generic; using System.Text; ...

  5. SSIS 学习之旅 数据同步

    这一章 别人也有写过但是我觉得还是写写比较好.数据同步其实就是想仿照 数据库的发布订阅功能 第一章:SSIS 学习之旅 第一个SSIS 示例(一)(上) 第二章:SSIS 学习之旅 第一个SSIS 示 ...

  6. 我的Java开发学习之旅------>Java 格式化类(java.util.Formatter)基本用法

    本文参考: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html http://www.blogjava.net/ ...

  7. SSIS 学习之旅 第一个SSIS 示例(二)

    这一章还是继上一章例子 进行一些小的知识扩展.主要是为了让大家更快的上手SSIS. 概要设计:    1.按用户组生成CSV文件到Pending目录下,    2.移动Pending目录下的CSV文件 ...

  8. 我的Java开发学习之旅------>工具类:将播放器的进度值转换成相应的时间格式

    在我的博客<我的Java开发学习之旅------>Java 格式化类(java.util.Formatter)基本用法,地址:http://blog.csdn.net/ouyang_pen ...

  9. 学习实践:使用模式,原则实现一个C++数据库访问类

    一.概述 在我参与的多个项目中,大家使用libMySQL操作MySQL数据库,而且是源码即复用,在多个项目中有多套相同或相似的源码,这样的复用方式给开发带来了不变,而且libMySQL的使用比较麻烦, ...

随机推荐

  1. Centos下Vim编辑器基本配置

    设置 Vim编辑环境 配置 有两种方式: 1,是在/etc/vimrc 进行设置,这种设置方法会作用与所有登录到Linux环境下的用户.不建议使用. 2,在用户登录的 ~ 目录下创建一个 .vimrc ...

  2. P1022 计算器的改良

    P1022 计算器的改良 题目背景 NCL 是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在该公司某型号的计算器上加上解一元一次方程的功能.实验室将这个任务交给 ...

  3. python 类与对象解析

    类成员:    # 字段        - 普通字段,保存在对象中,执行只能通过对象访问        - 静态字段,保存在类中,  执行 可以通过对象访问 也可以通过类访问            # ...

  4. Xcode关闭警告

    对于关闭某个警告,如果需要全局关闭的话,直接在Other C Flags里写 -Wno-...就行了,比如 -Wextra -Wno-sign-compare 就是一个常见的组合.如果相对某几个文件开 ...

  5. RabbitMQ基础篇

    介绍 RabbitMQ 是一个消息中间件:它接收并转发消息.您可以把它想象为一个邮局:当您把需要寄出的邮件投递到邮箱,邮差最终会把邮件送给您的收件人.在这个比喻中,RabbitMQ 就是一个邮箱,也可 ...

  6. 翻译:GLSL的顶点位移贴图

    翻译:GLSL的顶点位移贴图 翻译自: Vertex Displacement Mapping using GLSL 译者: FreeBlues 说明: 之所以选择这篇文档, 是因为现在但凡提到位移贴 ...

  7. soj1010. Zipper

    1010. Zipper Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Given three strings, yo ...

  8. Hadoop2.6.0在CentOS 7中的集群搭建

    我这边给出我的集群环境是由一台主节点master和三台从节点slave组成: master     192.168.1.2 slave1       192.168.1.3 slave2       ...

  9. tmux终端工具

    本文原始地址:http://www.cnblogs.com/chinas/p/7094172.html,转载请注明出处,谢谢!!! 1.介绍 tmux(终端复用工具):一个很有趣的工具,类似GNU S ...

  10. 【前端】直击源头的让你3秒理解并且会用Jsonp!!!

    1. 同源策略 ajax之所以需要“跨域”,罪魁祸首就是浏览器的同源策略.即,一个页面的ajax只能获取这个页面相同源或者相同域的数据. 如何叫“同源”或者“同域”呢?——协议.域名.端口号都必须相同 ...