C# 从FTP服务器下载文件到本地
来源:互联网 发布:怎么在淘宝上开通花呗 编辑:程序博客网 时间:2024/05/18 15:05
static void Main(string[] args) { //ftp服务器路径 string ftpServer = System.Configuration.ConfigurationSettings.AppSettings["FtpServer"].ToString(); //ftp本地路径 string ftpDefaultUrl = System.Configuration.ConfigurationSettings.AppSettings["FtpDefaultUrl"].ToString(); //登入到ftp的账号 string ftpUserName = System.Configuration.ConfigurationSettings.AppSettings["LoginID"].ToString(); //登入到ftp的密码 string ftpUserPwd = System.Configuration.ConfigurationSettings.AppSettings["LoginPWD"].ToString(); //下载后的文件存放路径 string downloadUrl = System.Configuration.ConfigurationSettings.AppSettings["DownloadPath"].ToString(); //需要下载的文件名 string fileName = "test.txt"; //需要现在的文件在ftp上的完整路径 string fileUploadPath = ftpServer + ftpDefaultUrl; Uri uri = new Uri(fileUploadPath+"/"+fileName); //下载后存放的路径 string FileName = Path.GetFullPath(downloadUrl) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath); //创建文件流 FileStream fs = null; Stream responseStream = null; try { //创建一个与FTP服务器联系的FtpWebRequest对象 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); //设置请求的方法是FTP文件下载 request.Method = WebRequestMethods.Ftp.DownloadFile; //连接登录FTP服务器 request.Credentials = new NetworkCredential(ftpUserName, ftpUserPwd); //获取一个请求响应对象 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //获取请求的响应流 responseStream = response.GetResponseStream(); //判断本地文件是否存在,如果存在,则打开和重写本地文件 if (File.Exists(FileName)) { fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite); } else { fs = File.Create(FileName); } if (fs != null) { int buffer_count = 65536; byte[] buffer = new byte[buffer_count]; int size = 0; while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0) { fs.Write(buffer, 0, size); } fs.Flush(); fs.Close(); responseStream.Close(); } } finally { if (fs != null) fs.Close(); if (responseStream != null) responseStream.Close(); } }
App.config
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="FtpServer" value="ftp://192.168.123.156/"/> <add key="FtpDefaultUrl" value="/Upload"/> <add key="LoginID" value="zs"/> <add key="LoginPWD" value="123@qwe"/> <add key="DownloadPath" value="/Download"/> </appSettings></configuration>
- C# 从FTP服务器下载文件到本地
- java 远程从FTP服务器下载文件到本地
- 将本地文件上传到远程FTP服务器和从远程FTP服务器下载文件到本地
- C#中从服务器中下载execl文件到本地
- 从FTP服务器读取文件到本地
- Java 从服务器下载文件到本地
- Java 从服务器下载文件到本地
- Java从服务器下载文件到本地
- 以命令方式从FTP 服务器下载文件到本地指定目录
- 以命令方式从FTP 服务器下载文件到本地指定目录
- 上传文件到服务器,从服务器下载文件到本地
- 下载(二)客户端通过C# 代码直接从服务器下载文件到本地
- ssm单文件下载ftp服务器到本地
- java代码实现从FTP服务器上拉取文件到本地
- (PHP)用cURL从本地上传文件到FTP服务器
- scp从服务器下载文件到本地或从本地上传文件到服务器
- 从远程服务器下载文件到本地服务器
- 客户端通过C# 代码直接从服务器下载文件到本地
- STL容器如何正确调用自己的erase方法
- 夏时制英国和中国的时差是多少?伦敦与北京时差是多少?
- Android 推送方案
- 手机浏览器兼容性测试结果概要 (WAP开发备注)
- java基础
- C# 从FTP服务器下载文件到本地
- Java中的构造器
- 孙鑫c++视频
- 学习建议
- opencv for android 编译
- Java 查看jdk源码-查找源代码位置
- C函数获取系统时间按照格式打印赋值
- 黑马程序员_Java基础_面向对象(内部类、异常1)
- vi的使用