一、WebRequestMethods.Ftp类:

表示可与 FTP 请求一起使用的 FTP 协议方法的类型。

  • Append​File  :  表示要用于将文件追加到 FTP 服务器上的现有文件的 FTP APPE 协议方法。
  • Delete​File    :表示要用于删除 FTP 服务器上的文件的 FTP DELE 协议方法。
  • Download​File    :表示要用于从 FTP 服务器下载文件的 FTP RETR 协议方法。
  • Get​Date​Timestamp    :表示要用于从 FTP 服务器上的文件检索日期时间戳的 FTP MDTM 协议方法。
  • Get​File​Size    :表示要用于检索 FTP 服务器上的文件大小的 FTP SIZE 协议方法。
  • List​Directory    :表示获取 FTP 服务器上的文件的简短列表的 FTP NLIST 协议方法。
  • List​Directory​Details    :表示获取 FTP 服务器上的文件的详细列表的 FTP LIST 协议方法。
  • Make​Directory    :表示在 FTP 服务器上创建目录的 FTP MKD 协议方法。
  • Print​Working​Directory    :表示打印当前工作目录的名称的 FTP PWD 协议方法。
  • Remove​Directory    :表示移除目录的 FTP RMD 协议方法。
  • Rename    :表示重命名目录的 FTP RENAME 协议方法。
  • Upload​File    :表示将文件上载到 FTP 服务器的 FTP STOR 协议方法。
  • Upload​File​With​Unique​Name    :表示将具有唯一名称的文件上载到 FTP 服务器的 FTP STOU 协议方法。

二、上传文件:

OpenFileDialog opFilDlg = new OpenFileDialog();
if (opFilDlg.ShowDialog() == DialogResult.OK)
{ ftp = new YBBFTPClass("hz.a.cn", "", "csp", "welcome", 0);
ftp.UploadFile(opFilDlg.FileName);
MessageBox.Show("上传成功");
}

/// <summary>
/// 文件上传
/// </summary>
/// <param name="filename">本地文件路径</param>
public void UploadFile(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + RemoteHost + "/" + fileInf.Name;
FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + RemoteHost + "/" + fileInf.Name));// 根据uri创建FtpWebRequest对象
reqFTP.Credentials = new NetworkCredential(RemoteUser, RemotePass); // ftp用户名和密码
reqFTP.KeepAlive = false; // 默认为true,连接不会被关闭, 在一个命令之后被执行
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定执行什么命令
reqFTP.UseBinary = true; // 指定数据传输类型
reqFTP.ContentLength = fileInf.Length; // 上传文件时通知服务器文件的大小 int contentLen;
FileStream fileStream = fileInf.OpenRead(); // 打开一个文件读取内容到fileStream中
contentLen = fileStream.Read(buffer, 0, buffer.Length); ;//从fileStream读取数据到buffer中 Stream requestStream = reqFTP.GetRequestStream();
// 流内容没有结束
while (contentLen != 0)
{
requestStream.Write(buffer, 0, contentLen);// 把内容从buffer 写入 requestStream中,完成上传。
contentLen = fileStream.Read(buffer, 0, buffer.Length);
} // 关闭两个流
requestStream.Close();
//uploadResponse = (FtpWebResponse)reqFTP.GetResponse();
fileStream.Close();
}

三、下载文件

1、核心代码

/// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath">本地目录</param>
/// <param name="fileName">远程路径</param>
public void DownloadFile(string filePath, string fileName)
{
FtpWebRequest reqFTP;
try
{
FileStream fileStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + RemoteHost + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(RemoteUser, RemotePass); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();//从ftp响应中获得响应流 //long cl = response.ContentLength;
byte[] buffer = new byte[1024];
int readCount; readCount = responseStream.Read(buffer, 0, buffer.Length);//从ftp的responseStream读取数据到buffer中
while (readCount > 0)
{
fileStream.Write(buffer, 0, readCount);//从buffer读取数据到fileStream中,完成下载
readCount = responseStream.Read(buffer, 0, buffer.Length);
} responseStream.Close();
fileStream.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

2、winform:、

FolderBrowserDialog fldDlg = new FolderBrowserDialog();
if (txtUpload.Text.Trim().Length > 0)
{
if (fldDlg.ShowDialog() == DialogResult.OK)
{
ftp.DownloadFile(fldDlg.SelectedPath, txtUpload.Text.Trim());
MessageBox.Show("下载成功");
}
}
else
{
MessageBox.Show("Please enter the File name to download");
}

3、webform弹出下载提示:

FtpClient _client = new FtpClient();
Stream stream = _client.OpenRead(FtpFilePath, FtpDataType.Binary); string FtpFilePath = Request.QueryString["FilePath"];
string _fname = Path.GetFileName(FtpFilePath);
Response.ContentType = "application/" + _fname.Split('.')[1];
Response.AddHeader("Content-disposition", "attachment; filename=" + _fname); byte[] buffer = new byte[10240];
int readCount;
do
{
readCount = stream.Read(buffer, 0, buffer.Length);
Response.OutputStream.Write(buffer, 0, readCount);//持续写入流
} while (readCount != 0); Response.OutputStream.Write(buffer, 0, buffer.Length); Response.End();

四、删除文件

string uri = "ftp://" + RemoteHost + "/" + fileName;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + RemoteHost + "/" + fileName)); reqFTP.Credentials = new NetworkCredential(RemoteUser, RemotePass);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile; 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();

完整代码参考:

http://www.cnblogs.com/hsiang/p/7247801.html

FtpWebRequest与FtpWebResponse完成FTP操作的更多相关文章

  1. (转)FTP操作类,从FTP下载文件

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  2. C# FTP操作

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP操作 { ...

  3. 关于FTP操作的功能类

    自己在用的FTP类,实现了检查FTP链接以及返回FTP没有反应的情况. public delegate void ShowError(string content, string title); // ...

  4. FtpHelper ftp操作类库

    FtpHelper ftp操作类库 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  5. C# FTP操作类的代码

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

  6. 【转载】C#工具类:FTP操作辅助类FTPHelper

    FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样.可以通过C#中的FtpWebRequest类.NetworkCredential类.We ...

  7. [转]C# FTP操作类

      转自 http://www.cnblogs.com/Liyuting/p/7084718.html using System; using System.Collections.Generic; ...

  8. 【C#】工具类-FTP操作封装类FTPHelper

    转载:http://blog.csdn.net/gdjlc/article/details/11968477 using System; using System.Collections.Generi ...

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

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

随机推荐

  1. Tornado异步IO

    Tornado提供了强大的异步IO机制,提高了服务器的响应能力. @tornado.web.asynchronous tornado默认在处理函数返回时关闭链接,@tornado.web.asynch ...

  2. resize定义元素尺寸大小

    为了增强用户体验,CSS3增加了很对的新属性,其中一个重要的属性就是resize,它允许用户通过拖动的方式改变元素的尺寸,到目前为止,主要用于可以使用overtflow属性的任何容器元素中 resiz ...

  3. 有序列表ol和定义列表dl,dt,dd

    有序列表是一种讲究排序列表结构,使用<ol>标签定义,其中包含多个<li>列表项目.一般网页设计中,列表结构可以互用有序或者无序类表标签.但是,在强调项目排序栏目中,选用有序列 ...

  4. 如何向Maven仓库(私服)中上传第三方jar包

    本文详细介绍如何向maven仓库中上传第三方jar包. 1.在本地maven安装路径中找到conf文件夹下面的setting.xml文件,里面有访问maven仓库的路径和账号.密码: 2.浏览器打开第 ...

  5. 解决:oracle+myBatis ResultMap 类型为 map 时返回结果中存在 timestamp 时使用 jackson 转 json 报错

    前言:最近在做一个通用查询单表的组件,所以 sql 的写法就是 select *,然后 resultType="map" ,然后使用 jackson @ResponseBody 返 ...

  6. Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework

    昨晚在 使用spring aop, 然后Tomcat启动的时候, 报了这么个错: Caused by: org.springframework.beans.factory.BeanCreationEx ...

  7. jedis、jedisPool、jedisCluster的使用方法

    jedis 连接redis(单机): 使用jedis如何操作redis,但是其实方法是跟redis的操作大部分是相对应的. 所有的redis命令都对应jedis的一个方法     1.在macen工程 ...

  8. 转:导出csv文件数字会自动变科学计数法的解决方法

    导出csv文件数字会自动变科学计数法的解决方法   其实这个问题跟用什么语言导出csv文件没有关系.Excel显示数字时,如果数字大于12位,它会自动转化为科学计数法:如果数字大于15位,它不仅用于科 ...

  9. 移动端实现上拉加载更多(使用dropload.js vs js)

    做下笔记,:移动端实现上拉加载更多,其实是数据的分段加载,在这里为了做测试我写了几个json文件作为分段数据: 方式一:使用dropload.js; 配置好相关参数及回调函数就可使用:代码如下 var ...

  10. opencv3.2.0图像处理之方框滤波boxFilter API函数

    /*.1.方框滤波:boxFilter函数(注:均值滤波是归一化后的方框滤波)*/ /*函数原型: void boxFilter(InputArray src, OutputArray dst, in ...