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用户 ...
随机推荐
- docker底层技术概览
docker解决了云计算环境难于分发并且管理复杂,而用KVM.Xen等虚拟化又浪费系统资源的问题.Docker最初是基于lxc构建了容器引擎,为了提供跨平台支持,后又专门开发了libcontainer ...
- Java基础进阶整理
Java学习笔记整理 本文档是我个人整理的,首先是想通过完成本文档更加扎实自己的基础加强对java语言的理解,然后就是想给入了门的同志们做下贡献. 当然,本文档主要是对java语言基础(当然还有很多基 ...
- Best Practices for Performance_3.Improving Layout Performance 优化布局
http://developer.android.com/training/improving-layouts/index.html 1. 优化布局层次 1) 每增加一个View或者布局,都会增加额 ...
- oracle中的日期:周月季年,首天未天。
SQL> SQL 前一小时数 FROM dual; 现在时间 当前小时数 前一小时数 ------------------- ---------- ---------- :: SQL> S ...
- Supervisor 安装
在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件.由于在linux中,每个系统与用户进行交流的界 ...
- git push error: A Contributor Agreement must be completed before uploading
因为是从官方版本库做的镜像,所以有些权限直接从官方同步到了本地. 今天,有同事执行git push操作,报错: 根据网上搜索的内容,在gerrit.config中[auth]中添加如下内容: [aut ...
- LINUX 磁盘如何分区
fdisk -l 可以查看当前磁盘 假设未分配磁盘为/dev/sdb size=10G fdisk /dev/sdb (m for help) 按照提示应该可以分区成功,注意一点 一个磁 ...
- 每天一个 Linux 命令(16):which命令
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置 ...
- socket listen参数中的backlog 的意义!
服务器监听时,在每次处理一个客户端的连接时是需要一定时间的,这个时间非常的短(也许只有1ms 或者还不到),但这个时间还是存在的.而这个backlog 存在的意义就是:在这段时间里面除了第一个连接请求 ...
- [源码]String StringBuffer StringBudlider(1String部分)
String /** The value is used for character storage. */ private final char value[]; /** Th ...