(转)FTP操作类,从FTP下载文件
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下载文件的更多相关文章
- PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )
/** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...
- C# FTP操作类的代码
如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...
- php的FTP操作类
class_ftp.php <?php /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ftp { public $off; // 返回操作状 ...
- c#FTP操作类,包含上传,下载,删除,获取FTP文件列表文件夹等Hhelp类
有些时间没发表文章了,之前用到过,这是我总结出来关于ftp相关操作一些方法,网上也有很多,但是没有那么全面,我的这些仅供参考和借鉴,希望能够帮助到大家,代码和相关引用我都复制粘贴出来了,希望大家喜欢 ...
- C#FTP操作类含下载上传删除获取目录文件及子目录列表等等
ftp登陆格式 : ftp://[帐号]:[密码]@[IP]:[端口] ftp://用户名:密码@FTP服务器IP或域名:FTP命令端口/路径/文件名 直接上代码吧,根据需要选择函数,可根据业务自己 ...
- FTP操作类
using System; using System.Collections.Generic; using System.Net; using System.IO; namespace HGFTP { ...
- 很实用的FTP操作类
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...
- FTP操作类的使用
FTP(文件传输协议) FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序 ...
- 在开发框架中使用FTP辅助类上传或者下载文件,方便管理附件内容
在有些系统应用里面,我们需要对应用服务器.数据库服务器.文件服务器进行分开,文件路径等信息存储在数据库服务器里面,但文件内容则存储在文件服务器里面,通过使用FTP进行文件的上传下载,从而实现更加高效的 ...
随机推荐
- 关于WCF的一些注意事项
1.服务代理,建立通道的方法,要注意及时关掉代理,因为服务设置有一个服务的最大连接数,超过这个连接数,则后面的连接将会等待,一直到超时,报错!! 2.在已有配置的基础上,利用代码更改终结点,如果重设了 ...
- C#DataGridView合计处理
网上查了一些关于合计的代码 ,但发现大都都不尽人意,就自己再根据资料改了一下. #region 合计 //调用方法示例 //HeJi heji = null; //heji = new HeJi(la ...
- Java 之 多线程编程
1.线程: a.由来:单任务OS -- 多任务OS b.进程:每一个进程对应一个应用程序,分配独立内存空间 c.线程:线程是进程内部的一个独立的执行分支 d.特点:共享内容地址空间,切换成本更低 2. ...
- 自定义adapter实现listview双列显示
package com.appshare; import java.util.ArrayList;import java.util.List; import android.content.Conte ...
- Linux将文件拷到u盘
1:终端中获取root后,使用fdisk -l来查看位置 2:挂载u盘:mount /路径 /mnt cd /mnt可进入u盘,ls可查看文件 3:复制:cp /(文件路径) /mnt 4:退出u盘: ...
- AOP动态代理解析5-cglib代理的实现
CGLIB是一个强大的高性能的代码生成包.它广泛地被许多AOP的框架使用,例如Spring AOP和dynaop,为他们提供方法的Interception(拦截).EasyMock和jMock是通过使 ...
- 领域模型(domain model)&贫血模型(anaemic domain model)&充血模型(rich domain model)
领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专注于分析问题领域本身,发掘重要的业务领域概念,并建立业务领域概念之间的关系. 贫血模型是指使用的领域对象中只有s ...
- 如何解决在Ue4编辑器中查看中文注释为乱码的情况
一般人都会在自己定义的函数后面添加注释,Ue4会在蓝图编辑器中显示这些注释,这是一个相当棒的设定. 但是如果这些注释是中文的话,在蓝图编辑器中就会显示乱码. 如何解决呢? 只需要把你的文件用UTF-8 ...
- Docker(linux container) 所依赖的底层技术
1 Namespace 用来做PID的隔离,有了namespace,在docker container里头看来,就是一个完整的linux的世界.在host看来,container里的进程,就是一个普通 ...
- 注册事件处理程序onclick和addEventListener、attachEvent
一.设置HTML标签属性为事件处理程序(注意和下面的设置javascript对象属性为事件处理程序是不同的) 用于设置文档元素事件处理程序属性也能化成对应的HTML标签的属性.如果这样做,属性值应该是 ...