C# Ftp Client 基本操作
C# Ftp Client 上传、下载与删除
简单介绍一下Ftp Client 上传、下载与删除,这是目前比较常用的命令,各个方法其实都差不多,重点是了解Ftp命令协议。
1.建立连接
public static string Connect(string path, string Login, string Password)
{
try
{
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(path)); //指定命令
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); //
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); return "FTP连接成功";
}
catch(Exception ex)
{
return "FTP连接失败," + ex.Message;
} }
2.上传文件
public static string UploadFile(string filename, string FtpPath, string Login, string Password)
{
try {
FileInfo fileInf = new FileInfo(filename); //判断是否有上级目录 string uri = "ftp://" + Path.Combine(FtpPath, fileInf.Name); // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri)); // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为kb int buffLength = ; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流(System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的kb contentLen = fs.Read(buff, , buffLength); // 流内容没有结束 while (contentLen != ) { // 把内容从file stream 写入upload stream strm.Write(buff, , contentLen); contentLen = fs.Read(buff, , buffLength); } // 关闭两个流 strm.Close(); fs.Close();
//Successinfo
return string.Format("本地文件{0}已成功上传", fileInf.Name);
} catch (Exception ex) {
//ErrorInfo
return "上传失败" + ex.Message;
} }
3.下载文件
public static string DownloadFile(string fileDownPath, string fileName, string FtpPath, string Login, string Password)
{
try
{
string onlyFileName = Path.GetFileName(fileName); string newFileName = fileDownPath + onlyFileName; if (File.Exists(newFileName)) { string errorinfo = string.Format("文件{0}在该目录下已存在,无法下载", fileName); return errorinfo;
} string uri = "ftp://" + Path.Combine(FtpPath, fileName); // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri)); // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = ; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, , bufferSize); FileStream outputStream = new FileStream(newFileName, FileMode.Create); while (readCount > )
{ outputStream.Write(buffer, , readCount); readCount = ftpStream.Read(buffer, , bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); //Successinfo return string.Format("服务器文件{0}已成功下载", fileName); } catch (Exception ex) {
//errorinfo
return string.Format("因{0},无法下载", ex.Message); } }
4.删除文件
public static string DeleteFile(string fileName, string FtpPath, string Login, string Password)
{
try
{
FileInfo fileInf = new FileInfo(fileName); string uri = "ftp://" + Path.Combine(FtpPath, fileInf.Name); // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri)); // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); //Successinfo return string.Format("文件{0}已成功删除", fileInf.Name);
} catch (Exception ex)
{
//ErrorInfo
return string.Format("文件因{0},无法删除", ex.Message);
} }
C# Ftp Client 基本操作的更多相关文章
- Ubuntu Filezilla FTP Client 安装
/************************************************************************************* * Ubuntu File ...
- 使用 FileZilla FTP Client连接Vsftpd在执行LIST命令后提示连接超时
使用 FileZilla FTP Client 连接 Vsftpd在执行LIST命令后提示连接超时. vi /etc/vsftpd/vsftpd.conf 添加: #开启被动模式 pasv_enabl ...
- csharp: FTP Client Library using System.Net.FtpWebRequest
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- FileZilla FTP Client
FileZilla Client是一个快速.实用.多功能和界面直观的免费的FTP客户端,虽然它是免费软件,可功能却一点也不含糊,比起那些共享软件来有过之而无不及,在新的版本中作者改进了手动下载的界面和 ...
- csharp: FTP Client Library using System.Net.FtpClient and FluentFTP,测试中存在的BUG修正
https://netftp.codeplex.com/ /// <summary> /// Gets a file listing from the server. Each FtpLi ...
- csharp:FTP Client Library using FtpWebRequest or Sockets
https://netftp.codeplex.com/SourceControl/latest http://ftplib.codeplex.com/ https://www.codeproject ...
- FTP Client
1: /// <summary> 2: /// FTP 管理类 3: /// </summary> 4: public class FTPManage 5: { 6: priv ...
- 【FileZilla FTP Client】文件与服务器操作客户端
跨平台的FTP,FTPS和SFTP客户端 可以断点续传进行上传.下载(需要服务器支持). 自定义命令. 可进行站点管理.
- Spring Boot Ftp Client 客户端示例支持断点续传
本章介绍 Spring Boot 整合 Ftpclient 的示例,支持断点续传 本项目源码下载 1 新建 Spring Boot Maven 示例工程项目 注意:是用来 IDEA 开发工具 File ...
随机推荐
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LA 4094 WonderTeam 构造
题意: 一共有\(n\)支队伍参加比赛,每两支队伍比赛两场,主客场各一场. 胜场得\(3\)分,平局得1分,败场不得分. 一支得分为\(p\)的队伍的排名\(=\)分数大于\(p\)的队伍数\(+1\ ...
- 《Scrum实战》第3次课【富有成效的每日站会】作业汇总
1组 崔儒: http://kecyru.blog.163.com/blog/static/2741661732017626101944123/ 2017-07-26 孟帅: http://www.c ...
- luogu2763 试题库问题
倘若某个试题已经被选到某个类型里了,那么它就不可再被选进别的类型了. 所以,对于每个类型,我们将其与汇连边,权值是它的要求的题目数量. 对于每个题目,我们将源与其连边,权值是1,代表只能用一次.然后再 ...
- 再谈H2的MVStore与MVMap
对H2的[MVStore]: http://www.cnblogs.com/simoncook/p/5188105.html 这篇文章的补充. 概述 我们通常用的map,比如HashMap Linke ...
- Latex数学公式表
1. Latex的两种公式模式 行间(inline)模式:即在正文中插入数学内容.行间公式用$ … $ 独立(display)模式:独立成行,可以有或没有编号.无编号用\ [ … \ ] 2.基本元素 ...
- javascript学习笔记-数据类型
一 数据类型 基本类型:undefined,null,boolean,number,string 保存在栈内存中 占用空间固定 变量直接从栈内存中存取的是该值 引用类型: ...
- WebApplicationContextUtils源码
package org.springframework.web.context.support; import javax.servlet.ServletContext; import javax.s ...
- Graham扫描法 --求凸包
前言: 首先,什么是凸包? 假设平面上有p0~p12共13个点,过某些点作一个多边形,使这个多边形能把所有点都“包”起来.当这个多边形是凸多边形的时候,我们就叫它“凸包”.如下图: 然后,什么是凸包 ...
- iOS----精品开源库-开发强力助攻
30个精品iOS开源库,超强助攻 你不会想错过他们,真的. 我爱开源. 文章的尾部你会看到一个太长不看的版本——一个简单的列表,只有标题和到项目的链接.如果你发现这篇文章是有用的,把它和你的iOS开 ...