zoukankan      html  css  js  c++  java
  • 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(" ");
    flag = true;
    line = reader.ReadLine();
    continue;
    }
    line = reader.ReadLine();
    }
    reader.Close();
    response.Close();
    if (flag)
    {
    result.Remove(result.ToString().LastIndexOf(" "), 1);
    return result.ToString().Split(' ');
    }
    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(" ");
    flag = true;
    line = reader.ReadLine();
    continue;
    }
    line = reader.ReadLine();
    }
    reader.Close();
    response.Close();
    if (flag)
    {
    result.Remove(result.ToString().LastIndexOf(" "), 1);
    return result.ToString().Split(' ');
    }
    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;

    }
    }
    }

  • 相关阅读:
    【转】Windows Socket网络编程(二)套接字编程原理
    获取本地IP地址,并在IP CONTROL控件中显示出来
    PAT 1021 Deepest Root[并查集、dfs][难]
    1025 PAT Ranking[排序][一般]
    PAT 1028 List Sorting[排序][一般]
    PAT 1023 Have Fun with Numbers[大数乘法][一般]
    PAT 1026 Table Tennis[比较难]
    Andrew NgML第十章应用机器学习的建议
    PAT 1020 Tree Traversals[二叉树遍历]
    PAT 1022 Digital Library[map使用]
  • 原文地址:https://www.cnblogs.com/c-x-a/p/6211025.html
Copyright © 2011-2022 走看看