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用户 ...
随机推荐
- oracle中Update方法
1.两表(多表)关联update -- 被修改值由另一个表运算而来 update customers a set city_name=(select b.city_name from tmp_cust ...
- js api 实现钉钉免登
js api 实现钉钉免登,用于从钉钉微应用跳转到企业内部的oa,erp等,我刚刚实施完了我公司的这个功能,钉钉用起来还不错. 1 js api 实现钉钉免登,页面配置. <title>利 ...
- OAF_开发系列29_实现OAF中批次处理迭代器RowSet/RowSetIterator(案例)
20150814 Created By BaoXinjian
- AMap公交线路查询
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- fuser 命令小结
fuser 概述 fuser命令是用来显示所有正在使用着指定的file, file system 或者 sockets的进程信息. 例一: #fuser –m –u /mnt/usb1 /mnt/us ...
- Java实现购物车功能:方式一:存放在session中.方式二:存储在数据库中
//将购物车产品加入到cookie中,方式同浏览记录.Java实现购物车,方式一(简易版):存储在session中.这种方式实现还不严谨,大家看的时候看思路即可.(1). JSP页面中,选择某一款产品 ...
- Spark On YARN使用时上传jar包过多导致磁盘空间不够。。。
今天测试过程中发现YARN Node变成Unhealthy了,后来定位到硬盘空间不够..... 通过查找大于100M的文件时发现有N多个spark-assembly-1.4.0-SNAPSHOT-ha ...
- jQuery 中对 CommonJs 的支持处理
jQuery 中对 CommonJs提供了直接支持,可以在 CommonJs模块中直接引用 jQuery 对象,这是如何实现的呢? 从 factory 函数说起 说先看 jQuery 的主体函数定义, ...
- JavaScript笔记:变量及其作用域
一.变量的定义及声明 在javascript中变量仅仅是用来保存值的一个占位符而已,定义变量时要使用关键字var后跟一个变量名,如下所示: var message; //定义一个变量message,像 ...
- spring下载及部署
网址http://repo.spring.io/release/org/springframework/spring Bean配置项 Id : 在IOC容器中此Bean唯一标识 Class : 要实例 ...