ftp
1.url的确定
string ftpServerIP = "29.184.249.98";
string path=new Uri("ftp://"+ftpServerIP+"/").ToString();
2. 创建连接
FtpWebRequest reqFTP;
public void Connection(string path)
{
try
{
//根据uri创建FTPWebRequest对象
reqFTP=(FtpWebRequest)FtpWebRequest.Create(new Uri(path));
//指定数据传输类型
reqFTP.UseBinary=true;
// 指定数据传输类型
reqFTP.UsePassive = false;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(username, password);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
3
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ftptest
{
class FtpMathod
{
const string username = "spc";
const string password = "iscxans9750";
FtpWebRequest reqFTP;
public string[] GetDeleteFolderArray(string path)
{
FtpDirInfo ftpDirInfo = new FtpDirInfo();
string[] deleteFolders;
// StringBuilder 字符串变量(非线程安全)
StringBuilder result = new StringBuilder();
try
{
Connecion(path);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
String line = reader.ReadLine();
bool flag = false;
while (line != null)
{
FileStruct f = new FileStruct();
f = ftpDirInfo.GetList(line);
String fileName = f.Name;
if (f.IsDirectory)
{
result.Append(fileName);
result.Append("\n");
flag = true;
line = reader.ReadLine();
continue;
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
if (flag)
{
result.Remove(result.ToString().LastIndexOf("\n"), 1);
return result.ToString().Split('\n');
}
else
{
deleteFolders = null;
return deleteFolders;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "获取文件夹数组过程中出现异常");
deleteFolders = null;
return deleteFolders;
}
}
//获取子文件数组
public string[] GetDeleteFileArray(string path)
{
FtpDirInfo ftpDirInfo = new FtpDirInfo();
List<string> ftpList = new List<string>();
string[] DeleteFiles;
StringBuilder result = new StringBuilder();
try
{
Connecion(path);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
string line = reader.ReadLine();
bool flag = false;
while (line != null)
{
ftpList.Add(line);
FileStruct f = new FileStruct();
f = ftpDirInfo.GetList(line);
String fileName = f.Name;
//排除非文件夹
if (!f.IsDirectory)
{
result.Append(fileName);
result.Append("\n");
flag = true;
line = reader.ReadLine();
continue;
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
if (flag)
{
result.Remove(result.ToString().LastIndexOf("\n"), 1);
return result.ToString().Split('\n');
}
else
{
DeleteFiles = null;
return DeleteFiles;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "获取文件数组过程中出现异常");
DeleteFiles = null;
return DeleteFiles;
}
}
public void DeleteFile(string path)
{
try
{
Connecion(path);
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "删除文件过程中出现错误");
}
}
public void DeleteDir(string path)
{
try
{
string[] folderArray = GetDeleteFolderArray(path);
string[] fileArray = GetDeleteFileArray(path);
ArrayList folderArrayList = new ArrayList();
ArrayList fileArrayList = new ArrayList();
//重新构造存放文件夹的数组(用动态数组实现)
for (int i = 0; i < folderArray.Length; i++)
{
if (folderArray[i] == "." || folderArray[i] == ".." || folderArray[i] == "")
{
}
else
{
folderArrayList.Add(folderArray[i]);
}
}
//重新构造存放文件的数组(用动态数组实现)
for (int i = 0; i < fileArray.Length; i++)
{
if (fileArray[i] == "")
{
}
else
{
fileArrayList.Add(fileArray[i]);
}
}
if (folderArrayList.Count == 0 && fileArrayList.Count == 0)
{
DeleteFolder(path);
}
else if (folderArrayList.Count == 0 && fileArrayList.Count != 0)
{
for (int i = 0; i < fileArrayList.Count; i++)
{
string fileUri = path + "/" + fileArrayList[i];
DeleteFile(fileUri);
}
DeleteFolder(path);
}
else if (folderArrayList.Count != 0 && fileArrayList.Count != 0)
{
for (int i = 0; i < fileArrayList.Count; i++)
{
string fileUri = path + "/" + fileArrayList[i];
DeleteFile(fileUri);
}
for (int i = 0; i < folderArrayList.Count; i++)
{
string dirUri = path + "/" + folderArrayList[i];
DeleteDir(dirUri);
}
DeleteFolder(path);
}
else if (folderArrayList.Count != 0 && fileArrayList.Count == 0)
{
for (int i = 0; i < folderArrayList.Count; i++)
{
string dirUri = path + "/" + folderArrayList[i];
DeleteDir(dirUri);
}
DeleteFolder(path);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "删除目录过程中出现异常");
}
}
public void DeleteFolder(string path) //path为所要删除的文件夹的全路径
{
try
{
Connecion(path);
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "删除文件夹过程中出现错误");
}
}
public void CreateFolder(string path) //path为所要删除的文件夹的全路径
{
try
{
Connecion(path);
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "删除文件夹过程中出现错误");
}
}
public void Connecion(string path)
{
try
{ // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// 指定数据传输类型
reqFTP.UseBinary = true;
// 指定数据传输类型
reqFTP.UsePassive = false;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(username, password);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void UploadFile(string path, string dir) //path为所要删除的文件夹的全路径
{
try
{
Connecion(path);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
reqFTP.Credentials = new NetworkCredential(username, password);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(dir);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
reqFTP.ContentLength = fileContents.Length;
Stream requestStream = reqFTP.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
response = (FtpWebResponse)reqFTP.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void GetFileListArray(string path, string dir)
{
try
{
GetFtpFileList(path);
if (!GetFtpFileList(path).Contains("ass"))
{
path += "ass";
CreateFolder(path);
}
else
{
path += "ass";
}
if (!GetFtpFileList(path).Contains("20101002"))
{
path += "/" +"20101002";
CreateFolder(path);
}
else
{
path += "/" + "20101002";
}
path += "/" + "1.html";
UploadFile(path, dir);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "获取文件数组过程中出现异常");
}
}
public List<string> GetFtpFileList(string path)
{
List<string> ftpList = new List<string>();
try
{
FtpDirInfo ftpDirInfo = new FtpDirInfo();
StringBuilder result = new StringBuilder();
Connecion(path);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
string line = reader.ReadLine();
while (line != null)
{
ftpList.Add(line);
FileStruct f = new FileStruct();
f = ftpDirInfo.GetList(line);
String fileName = f.Name;
//排除非文件夹
if (!f.IsDirectory)
{
line = reader.ReadLine();
continue;
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ftpList;
}
}
}
ftp的更多相关文章
- 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案
前言: 这几天终于还是按耐不住买了一个月阿里云的学生优惠.只要是学生,在学信网上注册过,并且支付宝实名认证,就可以用9块9的价格买阿里云的云服务ECS.确实是相当的优惠. 我买的是Windows S ...
- win7下利用ftp实现华为路由器的上传和下载
win7下利用ftp实现华为路由器的上传和下载 1. Win7下ftp的安装和配置 (1)开始->控制面板->程序->程序和功能->打开或关闭Windows功能 (2)在Wi ...
- Java实现FTP文件与文件夹的上传和下载
Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...
- centos下开启ftp服务
如果要ftp访问linux需要安装ftp服务,vsftpd是Linux下比较好的的FTP服务器. 一.检查安装vsftp //检查是否安装vsftpd rpm -qa | grep vsftpd // ...
- 解决开启服务器防火墙导致ftp不能连接的问题
在防火墙设置的"高级"选项卡中的"网络连接设置"--"本地连接"--"设置"中添加了"FTP服务器" ...
- centos6.5 nginx-1.8.0和ftp搭建图片服务器
一.Nginx的安装步骤 1.Nginx安装环境: gcc: 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c+ ...
- Jenkins配置MSBuild实现自动部署(MSBuild+SVN/Subversion+FTP+BAT)
所要用到的主要插件: [MSBuild Plugin] 具体操作: 1.配置MSBuild的版本 [系统管理]->[Global Tool Configuration]->[MSBuild ...
- [CentOs7]搭建ftp服务器(2)——添加用户
摘要 上篇文章完成了ftp服务器的安装与匿名访问的内容,当然出于安全的考虑是不允许匿名访问服务器的,所以就有了本篇的内容 ,为ftp服务器添加用户,用改用户进行访问. vsftpd添加用户 FTP用户 ...
随机推荐
- C#:生成短网址
/// <summary> /// 短网址应用 ,例如QQ微博的url.cn http://url.cn/2hytQx /// </summary> /// <param ...
- [I2C]I2C总线协议图解
转自:http://blog.csdn.net/w89436838/article/details/38660631 1 I2C总线物理拓扑结构 I2C 总线在物理连接上非常简单,分别由S ...
- AMD高级应用(翻译)
Dojo now supports modules written in the Asynchronous Module Definition (AMD) format, which makes co ...
- RT-Thread的线程(任务)处理【RT-Thread学习笔记 2】
RT-Thread中使用线程这个概念,而不是任务.两者相似,我在这里把他的线程当作任务来理解了 1.任务处理: 动态任务相关API 创建任务:rt_thread_create函数,创建任务之后会返回r ...
- Hibernate5.2关联关系之单向多对一(二)
Hibernate5.2之单向一对多(二) 一. 简介 在本篇博文中笔者会在上一篇博客的代码基础上进行修改,本篇文章将介绍单向的一对多. 二. hbm文件的方式 Customer.hbm.xml &l ...
- 扫描二维码自动识别手机系统(Android/IOS)
移动互联网发展迅速,各种APP的开发都会推出多个版本(多终端),比如:iPhone版.iPad版.Android版.有些APP还会考虑覆盖到多个国家(国际化),比如:中文版.英文版.日文版.韩文版等. ...
- linux基础学习
1.默认不写端口号的就是80端口 本地ip:localhost或者127.0.0.1 2.用户管理 id和whoami:可以查看当前用户 who和w查看当前已经登录的用户 (1)添加用户,用户默认的家 ...
- memcpy和memmove
memcpy函数 函数原型 void *memcpy(void *dest, const void *src, size_t n); dest:目标地址 src: 起始地址 n: 字节数 头文件 st ...
- flume整合kafka
# Please paste flume.conf here. Example: # Sources, channels, and sinks are defined per # agent name ...
- Ice分布式程序设计—IceBox(Hello World Application)
忙了三天,总算浏览完此书.藉此记下 Ice 的 IceBox 服务框架. 在此用 IceBox 框架写 Hello World 程序,即以载体来体现其特性. 第一步:编写 Slice 文件,映射生成 ...