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用户 ...
随机推荐
- JQuery 1.*速成版之二
过滤选择器简称:过滤器.它其实也是一种选择器,而这种选择器类似与 CSS3(http://t.mb5u.com/css3/)里的伪类,可以让不支持 CSS3 的低版本浏览器也能支持.和常规选择器一样, ...
- Xxtea加解密
转自:http://www.cnblogs.com/luminji/p/3406407.html 很有意思的一件事情,当我想要找 Xxtea 加解密算法的时候,发现了前同事(likui318)的代码, ...
- 脚本 用 scp 拷贝文件
#!/usr/bin/expect set proj_dir /home/jksong/NewsSpark/openid_for_commonid set tmp_data_dir $proj_dir ...
- 在shiro-cas中实现 Jasig-cas的Single Sign Out 功能
1 Single Sign Out 功能 即单点登出功能.也就是在任意子系统进行登出操作后,其他子系统会自动登出. 实际CAS登出的步骤为 所以每个子系统都需要实现一个sso登出响应. cas-cli ...
- [FFmpeg] ffmpeg 常用命令
1. 视频转换 比如一个avi文件,想转为mp4,或者一个mp4想转为ts. ffmpeg -i input.avi output.mp4 ffmpeg -i input.mp4 output.ts ...
- JNI开发中String转换chat*工具
char* Jstring2CStr(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsstring = env->FindCla ...
- jquery 中post 、get的同步问题
jquery 中post .get的同步问题 解决方法1: 在全局设置: $.ajaxSetup({ async : false }); 然后再使用post或get方法 $.get("reg ...
- Redis 配置文件详解
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写)## 1k => 1000 bytes# 1kb => ...
- 【转载】变更MySql数据存储路径的方法
1.在mysql安装目录下找到my.ini文件,更改#Path to the database root datadir="希望存放数据的地址" 2.将默认存放路径(一般为&quo ...
- XML文档
XML(Extensible Markuo Language)可标记扩展语言.它是一种以简单文本格式存储数据的方式,可以被任何计算机读取. XML文档里包含的元素都是可以自定义的. 1.XML文档声明 ...