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用户 ...
随机推荐
- python---dnspython
dnspython 是Python实现的一个DNS工具包,支持几乎所有的记录类型,可以用于查询,传输并动态更新ZONE信息,同时支持TSIG(事务签名)验证消息和EDNS0(扩展DNS).可以替代ns ...
- Spark Netty与Jetty (源码阅读十一)
spark呢,对Netty API又做了一层封装,那么Netty是什么呢~是个鬼.它基于NIO的服务端客户端框架,具体不再说了,下面开始. 创建了一个线程工厂,生成的线程都给定一个前缀名. 像一般的n ...
- es6 let
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python 类变量和实例变量
super(cls, inst) 获得的是 cls 在 inst 的 MRO 列表中的下一个类. 实例的属性存储在实例的__dict__中,类属性和方法存储在类的__dict__中.查找属性时,先检 ...
- IEnumerable<T>与IQueryable<T>以及.net的扩展方法
首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...
- emacs配置eslint 语法检查.找不到node解决
使用emacs配置eslint 当调用语法检查时报错 Suspicious state from syntax checker javascript-eslint: Checker javascrip ...
- 使用Spring配合Junit进行单元测试的总结
最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...
- Error:The network adaptor could not establish the connection问题的解决办法
最近在学习hibernate 5.0.4, 自然而然就需要使用数据库,由于本人工作中一直使用Oracle,于是在自己的电脑上安装了Oracle 12.1.0, 安装完成使用一直没有问题,突然有一天使 ...
- 模拟下载的进度条ProgressBar
作者:堕落的天使 图片效果 activity_main.xml(代码) <RelativeLayout xmlns:android="http://schemas.android.co ...
- 通过js获取cookie的实例及简单分析
今天碰到一个在firefox下swfupload 上传时session不一致问题 在一个项目遇到多文件上传时,firefox下,服务器端的session获取不一致问题. 解决办法: 解决办法:将ses ...