(4)FTP服务器下载文件
上一篇中,我们提到了怎么从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服务器下载文件的更多相关文章
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包
最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...
- 如何登陆FTP服务器下载文件
原文:https://jingyan.baidu.com/article/f25ef254134bef482c1b82c2.html 方法/步骤1 1 第一种介绍的方法是从计算机(我的电脑)上登陆 ...
- 匿名(无账号密码)从ftp服务器下载文件
public static String downFile(String ip,String ftpFileName,String savePath,String fileName) { FTPCli ...
- jenkins:从FTP服务器下载文件
lftp 账号:密码@192.168.207.2 lcd /home/eccore/app/chen get -c /基础运维共享文件/OK-TeamViewer14.2.2558.rar
- C#- FTP递归下载文件
c# ftp递归下载文件,找来找去这个最好.(打断点,一小处foreach要改成for) /// <summary> /// ftp文件上传.下载操作类 /// </summary& ...
- 通过cmd命令到ftp上下载文件
通过cmd命令到ftp上下载文件 点击"开始"菜单.然后输入"cmd"点"enter"键,出现cmd命令执行框 2 输入"ftp& ...
- 2.4 利用FTP服务器下载和上传目录
利用FTP服务器下载目录 import os,sys from ftplib import FTP from mimetypes import guess_type nonpassive = Fals ...
- Python之FTP多线程下载文件之分块多线程文件合并
Python之FTP多线程下载文件之分块多线程文件合并 欢迎大家阅读Python之FTP多线程下载系列之二:Python之FTP多线程下载文件之分块多线程文件合并,本系列的第一篇:Python之FTP ...
随机推荐
- Python 文件读和写
- 重新认识Box Model、IFC、BFC和Collapsing margins
尊重原创,转载自: http://www.cnblogs.com/fsjohnhuang/p/5259121.html 肥子John^_^ 前言 盒子模型作为CSS基础中的基础,曾一度以为掌握了I ...
- 【递推】BZOJ 4300:绝世好题
4300: 绝世好题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 564 Solved: 289[Submit][Status][Discuss] ...
- php正则过滤html标签、空格、换行符的代码,提取图片
$descclear = str_replace("r","",$descclear);//过滤换行 $descclear = str_replace(&quo ...
- struts2漏洞原理及解决办法
1.原理 Struts2的核心是使用的webwork框架,处理action时通过调用底层的getter/setter方法来处理http的参数,它将每个http参数声明为一个ONGL(这里是ONGL的介 ...
- SQL Server 2008 R2评估期已过的解决办法
SQL Server 2008 R2评估期已过的解决办法 发现问题 北美产品测试服每日随机任务没有刷新 每日随机任务是使用数据库作业定期执行操作,重置玩家随机任务项 排查问题 www.2cto. ...
- C# 序列化 Serialize 的应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 动态修改 NodeJS 程序中的变量值
如果一个 NodeJS 进程正在运行,有办法修改程序中的变量值么?答案是:通过 V8 的 Debugger 接口可以!本文将详细介绍实现步骤. 启动一个 HTTP Server 用简单的 Hello ...
- nested pop animation can result in corrupted navigation bar
nested pop animation can result in corrupted navigation barFinishing up a navigation transition in a ...
- POJ 2028
#include <iostream> #define MAXN 200 using namespace std; int mark[MAXN]; int main() { //freop ...