上一篇中,我们提到了怎么从FTP服务器下载文件。现在来具体讲述一下。

首先是路径配置。。

所以此处我们需要一个app.config来设置路径。

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="OnlineBankRequestFolder" value ="ftp://Online/Request/" />
<add key="OnlineBankResponseFolder" value ="ftp://Online/Response/" />
<add key ="ftpUser" value ="fsscftp"/>
<add key ="ftpPassword" value ="fssc123"/>
</appSettings>
</configuration>

另外在c#中需要获取路径

public string ftpUser = ConfigurationManager.AppSettings["ftpUser"];
public string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
public string downfolder = ConfigurationManager.AppSettings["OnlineBankRequestFolder"] + "/Request.txt";
public string savefolder = ConfigurationManager.AppSettings["OnlineBankResponseFolder"]+"/Response.xml";

然后还需要建立一个ftp请求。这时就需要建立一个ftpclient的类

  public class FtpClient
{
private string userName;
private string passWord;
private byte[] buffer = new byte[ * ]; public FtpClient(string userName, string passWord)
{
this.userName = userName;
this.passWord = passWord;
} /**/
/// <summary>
/// 创建ftp请求信息
/// </summary>
/// <param name="url">目标url地址</param>
/// <param name="useBinary">是否使用二进制传输</param>
private FtpWebRequest GetRequest(string url, bool useBinary)
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(url);
ftpRequest.Credentials = new NetworkCredential(userName, passWord);
ftpRequest.UseBinary = useBinary;
ftpRequest.KeepAlive = true;
return ftpRequest;
}
//下载文件
public void DownloadFile(string localFile, string remoteFile, bool useBinary)
{ FtpWebRequest request = GetRequest(remoteFile, useBinary);
request.Method = WebRequestMethods.Ftp.DownloadFile;
try
{
WriteStream(request.GetResponse().GetResponseStream(),File.Create(localFile));
}
catch (Exception ex)
{
throw (ex);
}
}
//将orgStream中的内容拷贝到desStream中
private void WriteStream(Stream orgStream, Stream desStream)
{
int num;
while ((num = orgStream.Read(buffer, , buffer.Length)) > )
{
desStream.Write(buffer, , num);
}
orgStream.Close();
desStream.Close();
}
}

这个类在下载时就会用到。另外还会用到一个方法,是得到保存路径。

  public string GetDataFileSavePath()
{
//FTP待导入数据文件存放的路径
string directory = savefolder;
//if (!(Directory.Exists(directory)))
// Directory.CreateDirectory(directory);
string fileName = "Cache_file.txt";
//保存路径
string savePath = System.IO.Path.Combine(directory, fileName);
//if (!File.Exists(savePath))
// File.Create(savePath);
return savePath;
}

好了。准备工作完成了。现在就可以下载了。

private void FTPDown(string txtpath,string strErrMsg="",string encodingCode = "GB2312", string strSplit = "|")
{
  string savePath = downfolder;
  if(txtpath.IndexOf("ftp")>=){
  //创建ftp请求信息
  var ftpRequest = new FtpClient(ftpUser, ftpPassword);
  savePath = GetDataFileSavePath();
  ftpRequest.DownloadFile(savePath, txtpath, true);
  }
}

这样FTP的文件就下载到本地的一个路径下面了。

(4)FTP服务器下载文件的更多相关文章

  1. 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  2. linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包

    最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...

  3. 如何登陆FTP服务器下载文件

    原文:https://jingyan.baidu.com/article/f25ef254134bef482c1b82c2.html 方法/步骤1   1 第一种介绍的方法是从计算机(我的电脑)上登陆 ...

  4. 匿名(无账号密码)从ftp服务器下载文件

    public static String downFile(String ip,String ftpFileName,String savePath,String fileName) { FTPCli ...

  5. jenkins:从FTP服务器下载文件

    lftp 账号:密码@192.168.207.2 lcd /home/eccore/app/chen get -c /基础运维共享文件/OK-TeamViewer14.2.2558.rar

  6. C#- FTP递归下载文件

    c# ftp递归下载文件,找来找去这个最好.(打断点,一小处foreach要改成for) /// <summary> /// ftp文件上传.下载操作类 /// </summary& ...

  7. 通过cmd命令到ftp上下载文件

    通过cmd命令到ftp上下载文件 点击"开始"菜单.然后输入"cmd"点"enter"键,出现cmd命令执行框 2 输入"ftp& ...

  8. 2.4 利用FTP服务器下载和上传目录

    利用FTP服务器下载目录 import os,sys from ftplib import FTP from mimetypes import guess_type nonpassive = Fals ...

  9. Python之FTP多线程下载文件之分块多线程文件合并

    Python之FTP多线程下载文件之分块多线程文件合并 欢迎大家阅读Python之FTP多线程下载系列之二:Python之FTP多线程下载文件之分块多线程文件合并,本系列的第一篇:Python之FTP ...

随机推荐

  1. 使用Java Service Wrapper在Linux下配置Tomcat应用

    前言 Java Service Wrapper是Tanuki Software的一个产品,可以将Java应用注册成Windows或Linux服务,使其可以随系统开机启动,同时可以监控Java应用的状态 ...

  2. 随机产生30个两个两位数相加的题目(java)

    编程思路: 1首先遇到JAVA产生随机数的问题. 2把产生的随机数设定范围. 3把划分的范围再分四个小区段分别对应四则运算法则加减乘除. 4打印输出. 题目源代码(Java) package coun ...

  3. 01.Apache FtpServer配置

    1.解压Apache FTPServer 将下载下来的压缩包(ftpserver-1.0.6.zip)解压到本地,其目录结构如下图: 2.修改users.properties 修改 \apache-f ...

  4. 【BZOJ】【2631】Tree

    LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...

  5. Matlab交集并集的实现

    >> a = [1 2 3 4 8 9]; >> b = [4 5 6 1] b = 4 5 6 1 >> c = intersect(a,b) c = 1 4 判 ...

  6. confusing uv

    in a projection u from letf to right is 0---1 in another proj u is the same but when i output u in r ...

  7. 在linux中使用phpize安装php扩展模块

    介绍:linux系统中,php安装成功后,在bin目录下会生成一个名叫phpize的可执行脚本,这个脚本的用途是动态安装php扩展模块.使用phpize脚本安装php扩展模块的好处:在安装php时没有 ...

  8. hbase表结构设计

    非常好的一个ppt   HBase Schema design: http://www.slideshare.net/cloudera/5-h-base-schemahbasecon2012

  9. 本地搭建Dubbo监控中心的安装步骤

    Dubbo监控中心的安装步骤 参考链接:http://blog.csdn.net/lichunan/article/details/40349645 一.从github上下载dubbo源码进行编译: ...

  10. list<T> 自定义比较器进行排序

    今天在研究List<T> 集合如何排序,我试过很多,但是都不行,然后看到msdn中的这个比较器排序,自己测试了代码,No Problem.给大家分享一下. 类型 T 的默认比较器按如下方式 ...