FtpWebRequest与FtpWebResponse完成FTP操作
一、WebRequestMethods.Ftp类:
表示可与 FTP 请求一起使用的 FTP 协议方法的类型。
- AppendFile : 表示要用于将文件追加到 FTP 服务器上的现有文件的 FTP APPE 协议方法。
- DeleteFile :表示要用于删除 FTP 服务器上的文件的 FTP DELE 协议方法。
- DownloadFile :表示要用于从 FTP 服务器下载文件的 FTP RETR 协议方法。
- GetDateTimestamp :表示要用于从 FTP 服务器上的文件检索日期时间戳的 FTP MDTM 协议方法。
- GetFileSize :表示要用于检索 FTP 服务器上的文件大小的 FTP SIZE 协议方法。
- ListDirectory :表示获取 FTP 服务器上的文件的简短列表的 FTP NLIST 协议方法。
- ListDirectoryDetails :表示获取 FTP 服务器上的文件的详细列表的 FTP LIST 协议方法。
- MakeDirectory :表示在 FTP 服务器上创建目录的 FTP MKD 协议方法。
- PrintWorkingDirectory :表示打印当前工作目录的名称的 FTP PWD 协议方法。
- RemoveDirectory :表示移除目录的 FTP RMD 协议方法。
- Rename :表示重命名目录的 FTP RENAME 协议方法。
- UploadFile :表示将文件上载到 FTP 服务器的 FTP STOR 协议方法。
- UploadFileWithUniqueName :表示将具有唯一名称的文件上载到 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();
完整代码参考:
FtpWebRequest与FtpWebResponse完成FTP操作的更多相关文章
- (转)FTP操作类,从FTP下载文件
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- C# FTP操作
using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP操作 { ...
- 关于FTP操作的功能类
自己在用的FTP类,实现了检查FTP链接以及返回FTP没有反应的情况. public delegate void ShowError(string content, string title); // ...
- FtpHelper ftp操作类库
FtpHelper ftp操作类库 using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- C# FTP操作类的代码
如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...
- 【转载】C#工具类:FTP操作辅助类FTPHelper
FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样.可以通过C#中的FtpWebRequest类.NetworkCredential类.We ...
- [转]C# FTP操作类
转自 http://www.cnblogs.com/Liyuting/p/7084718.html using System; using System.Collections.Generic; ...
- 【C#】工具类-FTP操作封装类FTPHelper
转载:http://blog.csdn.net/gdjlc/article/details/11968477 using System; using System.Collections.Generi ...
- PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )
/** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...
随机推荐
- WCF中的异常
一.考虑到安全因素,为了避免将服务端的异常发送给客户端.默认情况下,服务端出现异常会对异常屏蔽处理后,再发送到客户端.所以客户端捕捉到的异常都是同一个FaultException异常. 例如在服 ...
- 对于移动端浏览器touch事件的研究总结(4)判断手指滑动方向
最近有一些微信的项目,虽然页面很简单,但配合手势后的效果却是很不错的.最基本的效果就是手指向上滑,页面配合css3出现一个展开效果,手指向下滑将展开的内容按原路径收起.其实就是一个简单的判断手指滑动方 ...
- 【转】HttpServletRequestWrapper 实现xss注入
这里说下最近项目中我们的解决方案,主要用到commons-lang3-3.1.jar这个包的org.apache.commons.lang3.StringEscapeUtils.escapeHtml4 ...
- ASP.NET Core依赖注入
一.什么是依赖注入(Denpendency Injection) 这也是个老身常谈的问题,到底依赖注入是什么? 为什么要用它? 初学者特别容易对控制反转IOC(Iversion of Con ...
- 设计模式学习——抽象工厂模式(Abstract Factory Pattern)
现有一批装备(产品),分为不同的部位(上装.下装)与不同的等级(lv1.lv2).又有不同lv的工厂,只生产对应lv的全套装备. 代码实现上...本次写得比较偷懒,函数实现都写在头文件了.... 有些 ...
- xamarin.Android 选择本地图片、拍摄图片、剪裁图片
[Activity(Theme = "@style/MyStyleBottom")] public class SelectPicPopupWindow : Activity, I ...
- 慕课网 javascript深入浅出编程练习
任务 请在index.html文件中,编写arraysSimilar函数,实现判断传入的两个数组是否相似.具体需求: 1. 数组中的成员类型相同,顺序可以不同.例如[1, true] 与 [false ...
- 行内元素和块级元素的具体区别是什么?inline-block是什么?(面试题目)
一,行内元素与块级元素的区别: 1.行内元素与块级元素直观上的区别二.行内元素与块级元素的三个区别 行内元素会在一条直线上排列(默认宽度只与内容有关),都是同一行的,水平方向排列. 块级元素各占据一行 ...
- 深度研究Oracle数据库临时数据的处理方法
在Oracle数据库中进行排序.分组汇总.索引等到作时,会产生很多的临时数据.如有一张员工信息表,数据库中是安装记录建立的时间来保存的.如果用户查询时,使用Order BY排序语句指定按员工编号来排序 ...
- the detailed annotation of StringBuilder
public int capacity() 返回当前容量.容量指可用于最新插入字符的存储量,超过这一容量便需要再次分配. 返回: 当前容量. public int length() 返回长度(字符数) ...