上一篇中,我们提到了怎么从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. python-操作mssql数据库

    准备工作: cmd 命令行下安装pymssql: pip install pymssql 查询的数据库如下: 代码如下: #coding=utf-8 import pymssql class MSSQ ...

  2. Java 死锁诊断 -- 线程转储

    java线程转储 java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照.一个线程转储可能包含一个单独的线程或者多个线程.在多线程环境中,比如J2EE应用服务器,将会有许多线程和 ...

  3. 线段树--Color the ball(多次染色问题)

    K - Color the ball Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  4. NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏

    地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...

  5. matrix_last_acm_1

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96950#problem/A 题意:n个数初始ai,m次操作 ...

  6. OpenSSL 1.0.0生成p12、jks、crt等格式证书的命令个过程(转)

    OpenSSL 1.0.0生成p12.jks.crt等格式证书的命令个过程   此生成的证书可用于浏览器.java.tomcat.c++等.在此备忘!     1.创建根证私钥命令:openssl g ...

  7. 【bzoj1009】[HNOI2008]GT考试

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3018  Solved: 1856[Submit][Statu ...

  8. PHP操作数据库类

    <?php /** * 功能: 数据库操作类 . * 作者: 赵铭哲 * 日期: 2016-05-23 * 时间: 9:43 */ namespace ZH\DataBase; use \Exc ...

  9. 简单的表视图UITableView

    1.建一个Single View application 2.在故事板中放置一个Table View控件 3.在.h文件中加入协议 <UITableViewDataSource,UITableV ...

  10. 7 linux服务器程序规范

    1. Linux服务器程序一般以后台进程形式运行.后台进程又称守护进程(daemon),它没有控制终端,因而不会意外接收到用户输入.父进程通常为init(PID为1的进程)2. Linux服务器程序常 ...