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 ...
随机推荐
- 大数据技术之_09_Flume学习_Flume概述+Flume快速入门+Flume企业开发案例+Flume监控之Ganglia+Flume高级之自定义MySQLSource+Flume企业真实面试题(重点)
第1章 Flume概述1.1 Flume定义1.2 Flume组成架构1.2.1 Agent1.2.2 Source1.2.3 Channel1.2.4 Sink1.2.5 Event1.3 Flum ...
- MVC初级知识之——Routing路由
实例产品基于asp.net mvc 5.0框架,源码下载地址:http://www.jinhusns.com/Products/Download 我们注意到地址栏的URL是Home/Index 路由可 ...
- Oracle时间换算:日,月,周数,星期,年
http://blog.csdn.net/liangweiwei130/article/details/37930383 Oracle时间换算,留做记号!
- yum 安装php7.1
yum install http://rpms.remirepo.net/enterprise/remi-release-6.rpm yum -y install php71-php.x86_64 p ...
- Spring Boot—15SpringJPA
pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 在weblogic下部署找不到授权文件的解决方法
很多用户在weblogic上部署的时候,会遇到类似的报错信息,提示授权找不到,解决这个问题的思路如下: 第一步确定授权的没有过期, 客户如果修改了系统时间,会对授权生效产生影响,在进行操作前先将 ...
- linux 目录、文件名、logout、exit、shutdown、reboot、init 0、init 6、runlevel
/dev 设备目录/boot 系统启动目录/etc 配置文件保存目录/media./mnt./misc 挂载目录,实际可以自己随便定义一个目录作为挂载目录/opt 安装第三方软件位置,但现在 ...
- redis介绍(4)实战场景
redis我主要在两方面说明: 集群下的session的管理 Tomcat 与 DB之间缓存
- 无锁HashMap的原理与实现
转载自: http://coolshell.cn/articles/9703.html 在<疫苗:Java HashMap的死循环>中,我们看到,java.util.HashMap并不能直 ...
- ubuntu下配置JDK7环境变量
ubuntu下JDK配置本质上和win是一样的: 1.去官网下载JDK7,找jdk-7u21-linux-i586.tar.gz并下载:http://www.oracle.com/technetwor ...