(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 ...
随机推荐
- 【BZOJ】【1874】取石子游戏
SG函数 嗯博弈论入门题,关于SG函数这个东西可以去看VFK神犇的博客,讲的非常清楚Orz. 传送门:vfleaking.blog.163.com/blog/static/17480763420123 ...
- JS模板引擎 :ArtTemplate (1)
1.为什么需要用到模板引擎 我们在做前端开发的时候,有时候经常需要根据后端返回的json数据,然后来生成html,再显示到页面中去. 例如这样子: var data = [ {text: " ...
- Post 的数据被截断
原因: Form 域 POST 提交数据 100K(可能不是这个值) 限制的解决方案 因为微软这个限制是对表单内每个域(第一个控件)的限制.问题的解决办法是,对于一个需要发送大数据的域,在提交表单 ...
- 一些实用的 jQuery 技巧
jQuery如今已经成为Web开发中最流行的JavaScript库,通过jQuery和大量的插件,你可以轻松实现各种绚丽的效果. 本文将为你介绍一些实用的技巧,希望可以帮助你更加高效地使用jQuery ...
- Sqli-labs less 40
Less-40 本关的sql语句为SELECT * FROM users WHERE id=('$id') LIMIT 0,1 我们根据sql语句构造以下的payload: http://127.0. ...
- hadoop1.2.1配置文件
1)core-site.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" ...
- Grid分组特性
Ext.onReady(function () { Ext.define('personInfo', { extend: 'Ext. ...
- http status 汇总
http status 汇总 常见HTTP状态码 200 OK 301 Moved Permanently 302 Found 304 Not Modified 307 Temporary Redir ...
- HDU 1397 Goldbach's Conjecture(二分,查找素数)
题目意思很简单,就是找n=p1+p2的种类数,具体看题目吧. 次重点是查找一定范围内的素数: 重点是用二分查找,不然会超时!!! #include<stdio.h> #include< ...
- Webpack教程一
比较 如果你熟悉原来一系列的构建工具,grunt或者gulp之类的,这里有一篇webpack和他们比较的文章可以读一读. Webpack Compared 安装 先装好node和npm,因为webpa ...