using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Windows.Forms; namespace PCMDataImporter
{
/// <summary>
/// 封装了FTP的两个操作:下载文件Download()和获取FTP服务器上文件列表信息GetFileList()
/// </summary>
public class ftpOperater
{
private string ftpServerIP;
private string ftpUser;
private string ftpPwd; private SystemParaReader spReader; public ftpOperater()
{
spReader = new SystemParaReader();
this.ftpServerIP = spReader.GetSysParaValue("ftpServer");
this.ftpUser = spReader.GetSysParaValue("ftpUser");
this.ftpPwd = spReader.GetSysParaValue("ftpPwd");
} /// <summary>
/// 获取ftp服务器上的文件信息
/// </summary>
/// <returns>存储了所有文件信息的字符串数组</returns>
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close(); return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("获取文件信息失败:"+ex.Message,"操作失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
downloadFiles = null;
return downloadFiles;
}
} /// <summary>
/// 获取FTP上指定文件的大小
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>文件大小</returns>
public long GetFileSize(string filename)
{
FtpWebRequest reqFTP;
long fileSize = 0;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + filename));
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength; ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return fileSize;
} /// <summary>
/// 实现ftp下载操作
/// </summary>
/// <param name="filePath">保存到本地的文件名</param>
/// <param name="fileName">远程文件名</param>
public void Download(string filePath, string fileName)
{
FtpWebRequest reqFTP;
try
{
//filePath = <<The full path where the file is to be created.>>,
//fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
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)
{
MessageBox.Show(ex.Message);
}
}
}
}

(转)FTP操作类,从FTP下载文件的更多相关文章

  1. PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

  2. C# FTP操作类的代码

    如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...

  3. php的FTP操作类

    class_ftp.php <?php /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ftp { public $off; // 返回操作状 ...

  4. c#FTP操作类,包含上传,下载,删除,获取FTP文件列表文件夹等Hhelp类

    有些时间没发表文章了,之前用到过,这是我总结出来关于ftp相关操作一些方法,网上也有很多,但是没有那么全面,我的这些仅供参考和借鉴,希望能够帮助到大家,代码和相关引用我都复制粘贴出来了,希望大家喜欢 ...

  5. C#FTP操作类含下载上传删除获取目录文件及子目录列表等等

    ftp登陆格式  : ftp://[帐号]:[密码]@[IP]:[端口] ftp://用户名:密码@FTP服务器IP或域名:FTP命令端口/路径/文件名 直接上代码吧,根据需要选择函数,可根据业务自己 ...

  6. FTP操作类

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace HGFTP { ...

  7. 很实用的FTP操作类

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...

  8. FTP操作类的使用

    FTP(文件传输协议) FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序 ...

  9. 在开发框架中使用FTP辅助类上传或者下载文件,方便管理附件内容

    在有些系统应用里面,我们需要对应用服务器.数据库服务器.文件服务器进行分开,文件路径等信息存储在数据库服务器里面,但文件内容则存储在文件服务器里面,通过使用FTP进行文件的上传下载,从而实现更加高效的 ...

随机推荐

  1. C# DateTime时间格式转换为Unix时间戳格式

    double ntime=dateTimeToUnixTimestamp(DateTime.Now); long g1 = GetUnixTimestamp(); long g2 = ConvertD ...

  2. hdu 1517 博弈 **

    博弈题: 题意:2 个人玩游戏,从 1 开始,轮流对数进行累乘,直到超过一个指定的值. 解题思路:如果输入是 2 ~ 9 ,因为Stan 是先手,所以Stan 必胜如果输入是 10~18 ,因为Oll ...

  3. loj1011 状态压缩

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1011 思路:最近的开始做dp了...很明显的一道状态压缩题,dp[n][state]表 ...

  4. hive中大表join

    排序存储数据至BUCKETS,这样可以顺序进行join

  5. POJ 1008 Maya Calendar

    链接:http://poj.org/problem?id=1008 Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  6. 类中用const限定的成员函数

    本文转自http://blog.csdn.net/whyglinux/article/details/602329 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非 ...

  7. Android拓展系列(10)--使用Android Studio阅读整个Android源码

    之前一直在windows下用source insight阅读android源码,效果非常好.后来远程异地服务器,网络限制,一直用ssh + vim,现在主要还是以这种方式.最近发现一个不错的东西(早就 ...

  8. JDK BIO编程

    网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请求,通过三次握手建 ...

  9. 前端学PHP之文件操作(认真读读)

    前面的话 在程序运行时,程序本身和数据一般都存在内存中,当程序运行结束后,存放在内存中的数据被释放.如果需要长期保存程序运行所需的原始数据,或程序运行产生的结果,就需要把数据存储在文件或数据库.一般地 ...

  10. sql中保留到小数点后两位以及非空判断赋值为零

    SELECT a.dzzmc, a.dzzdm, a.px, CONVERT(decimal(18, 2), ISNULL(b.sjpfzdf, 0) * 0.6 + (ISNULL(a.zddfzd ...