/// <summary>
/// Ftp
/// </summary>
public class FtpFileOperation
{
private string _ftpIp;
private string _ftpUser;
private string _ftpPassword;
private FtpWebRequest _ftpWebRequest; public FtpFileOperation(string ftpIp, string ftpUser, string ftpPassword)
{
_ftpIp = ftpIp;
_ftpUser = ftpUser;
_ftpPassword = ftpPassword;
} /// <summary>
/// Connect
/// </summary>
/// <param name="ftpUrl"></param>
/// <returns></returns>
public bool Connect(string ftpUrl)
{
try
{
_ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(ftpUrl);
_ftpWebRequest.UseBinary = true;
_ftpWebRequest.Credentials = new NetworkCredential(_ftpUser, _ftpPassword);
return true;
}
catch (Exception ex)
{
Logger.Error(ex);
return false;
}
} /// <summary>
/// Download
/// </summary>
/// <param name="ftpFilePath"></param>
/// <param name="savePath"></param>
/// <returns></returns>
public bool Download(string ftpFilePath,string savePath)
{
try
{
if (Connect(String.Format("ftp://{0}/{1}", _ftpIp, ftpFilePath)))
{
FtpWebResponse ftpResponse = (FtpWebResponse)_ftpWebRequest.GetResponse();
Stream ftpStream = ftpResponse.GetResponseStream();
byte[] buffer = new byte[];
int readCount = ;
FileStream outputStream = new FileStream(savePath, FileMode.Create);
readCount = ftpStream.Read(buffer, , buffer.Length);
while(readCount > )
{
outputStream.Write(buffer, , readCount);
readCount = ftpStream.Read(buffer, , buffer.Length);
}
ftpStream.Close();
outputStream.Close();
ftpResponse.Close();
return true;
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
return false;
}
}

C# FTPHelper工具类的更多相关文章

  1. 【转载】C#工具类:FTP操作辅助类FTPHelper

    FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样.可以通过C#中的FtpWebRequest类.NetworkCredential类.We ...

  2. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  3. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  4. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  5. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  6. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  7. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  8. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  9. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

随机推荐

  1. 【165223&165218】结对感想——论如何与队友完美配合

    ★ 为什么要结对编程? 一.结对编程的优势 搭档的形式 平等.互补 高效率.共分享 相互影响.督促学习 方便复审与反馈 二.结对编程的方式 互换的角色 相同的目的 结对编程中有两个角色: (a)驾驶员 ...

  2. Python3 与 C# 面向对象之~继承与多态

      2.继承¶ 代码裤子:https://github.com/lotapp/BaseCode 在线编程:https://mybinder.org/v2/gh/lotapp/BaseCode/mast ...

  3. Linux网络那点事(CentOS、Ubuntu、Kali)

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 在Ubuntu18.04中,传统的配置/etc/network/interfa ...

  4. LOJ#2306 蔬菜

    补充一个题意不太清楚的地方:蔬菜坏掉是假设蔬菜都有标号,那么特定标号的蔬菜就会在特定时间坏掉.如果你及时卖了它们,那么那一天就不会有新的蔬菜坏掉. 结论1:如果我们知道了k天的答案,那么我们直接扔掉若 ...

  5. NOIP 提高组 2014 联合权值(图论???)

    传送门 https://www.cnblogs.com/violet-acmer/p/9937201.html 题解: 相关变量解释: int n; int fa[maxn];//fa[i] : i的 ...

  6. 函数:PHP将字符串从GBK转换为UTF8字符集iconv

    1. iconv()介绍 iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件.例如:从GB2312转换为UTF-8. iconv函数在php5中内置,GB字符集默认打开. 2. ic ...

  7. 使用postman测试hystrix

    当在浏览器发送多次请求检测hystrix的作用时,我们可以使用postman来自动发送多次请求: 1.将链接保存到一个collection中 2.点击runner 3.设定运行次数

  8. iptables防火墙设置

    ubuntu系统: apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙 # whereis iptables #查看系统是否安装防火墙可以看到:iptable ...

  9. python调用shell命令之三慷慨法

    preface: 忙于近期的任务,须要用到libsvm的一些命令.如在终端执行java svm_train train_file model_file. pythonsubset.py file tr ...

  10. python OrderDict

    # encoding: utf-8 import csv import collections d = {'banana':3,'apple':4,'pear':1,'orange':2} print ...