c#.net从ftp下载文件到本地   
  /*首先从配置文件读取ftp的登录信息*/
 string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString();
 string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName"].ToString();
 string FtpPassWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();
 string LocalFileExistsOperation = System.Configuration.ConfigurationManager.AppSettings["LocalFileExistsOperation"].ToString();
  Uri uri = new Uri(FtpPath);
string FileName = Path.GetFullPath(TempFolderPath) + 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, FtpPassWord);                 //获取一个请求响应对象
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                //获取请求的响应流
                responseStream = response.GetResponseStream();                 //判断本地文件是否存在,如果存在,则打开和重写本地文件                 if (File.Exists(FileName))                                {
                      if (LocalFileExistsOperation == "write")                       { 
                              fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);                       } 
                }                 //判断本地文件是否存在,如果不存在,则创建本地文件
                else                              {
                        fs = File.Create(FileName);
                }                 if (fs != null)
                {                     int buffer_count = ;
                    byte[] buffer = new byte[buffer_count];
                    int size = ;
                    while ((size = responseStream.Read(buffer, , buffer_count)) > )
                    {
                        fs.Write(buffer, , size);                     }
                    fs.Flush();
                    fs.Close();
                    responseStream.Close();
                }
            }
            finally
            {
                if (fs != null)
                    fs.Close();
                if (responseStream != null)
                    responseStream.Close();
            }  
 
 如果遇到服务器端下载路径包含汉字的话,就在配置文件web.config中的<system.web>节点下添加:
  <globalization   requestEncoding="GB2312"   responseEncoding="GB2312"   uiCulture="zh-CN"     culture="zh-CN"   fileEncoding="GB2312"   />
 
就可以解决了。

c#.net从ftp下载文件到本地的更多相关文章

  1. python从FTP下载文件

    #!/usr/bin/python # -*- coding: utf-8 -*- """ FTP常用操作 """ from ftplib ...

  2. 使用xshell从远程服务器下载文件到本地

    XSHELL工具上传文件到Linux以及下载文件到本地(Windows) Xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz.首先你的L ...

  3. 远程FTP下载文件

    现在存在以下环境: 远程服务器:192.168.1.107 用户名:dt 密码:dt123 需要从该服务器上下载文件到本地 1.登录(进入到那个目录登录的 ,文件就会被下载到该文件) ftp 192. ...

  4. java实现FTP下载文件

    ftp上传下载文件,是遵照ftp协议上传下载文件的,本例仅以下载文件为例. 重要的方法解释: 1.FTP功能相关依赖路径:org.apache.commons.net.ftp.*: 2.ftp默认端口 ...

  5. .Net 连接FTP下载文件报错:System.InvalidOperationException: The requested FTP command is not supported when using HTTP proxy

    系统环境: Windows + .Net Framework 4.0   问题描述: C#连接FTP下载文件时,在部分电脑上有异常报错,在一部分电脑上是正常的:异常报错的信息:System.Inval ...

  6. Java远程下载文件到本地(http协议和ssh2协议)

    Java中java.io包为我们提供了输入流和输出流,对文件的读写基本上都依赖于这些封装好的关于流的类中来实现.前段时间遇到了以下两种需求: 1.与某系统对接,每天获取最新的图片并显示在前端页面.该系 ...

  7. PHP实现远程下载文件到本地

    PHP实现远程下载文件到本地 投稿:hebedich 字体:[增加 减小] 类型:转载   经常写采集器发布接口需要使用到远程附件的功能,所以自己写了一个PHP远程下载文件到本地的函数,一般情况下已经 ...

  8. PHP CURL实现远程下载文件到本地

    <?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...

  9. C#FTP下载文件出现远程服务器返回错误: (500) 语法错误,无法识别命令

    如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的. 出现应用程序未处理的异常:2015/1/6 11:40:56 异常类型:WebExce ...

随机推荐

  1. framebufferfetch in mali multiple render targets mrt

    gl_LastFragColorARM https://www.khronos.org/registry/OpenGL/extensions/ARM/ARM_shader_framebuffer_fe ...

  2. learning scala How To Create Implicit Function

    println("Step 1: How to create a wrapper String class which will extend the String type") ...

  3. ooo's vimrc

    colorscheme evening set fillchars=vert:\ ,stl:\ ,stlnc:\ set nu set tabstop=4 set shiftwidth=4 set s ...

  4. wepy项目的学习

    使用Promise 开发实时编译 wepy build --watch 安装依赖 cd myproject npm install 安装(更新) wepy 命令行工具. npm install wep ...

  5. 原生table表格的使用

    近期公司让我修改一些之前的table标签写的页面,感觉对table相关的标签不是太熟悉,于是专门整理一下: 1.如果给td标签设置百分比宽度,比如有10列内容,我们却设置了每个单元格是30%的宽度,会 ...

  6. 在win10环境下IED配置spark项目

    eclipse在对spark的支持上并不友好,所以需要新下载并安装IntelliJ IDEA 2019.1.我下载安装的是专业版的,直接在网上搜索了破解码进行破解. 1. 配置java和scala I ...

  7. 前端逼死强迫症系列之javascript续集

    一.javascript函数 1.普通函数 function func(){ } 2.匿名函数 setInterval(function(){ console.log(123); },5000) 3. ...

  8. C语言中怎样定义能够保存16进制整数的变量

    可以通过int 或long int存储,16进制整数说到底还是整数,16进制只是一种记数方式.例如,int x=0x16;十六进制(hexadecimal)只是计算机中数据的一种表示方法,规则是“逢十 ...

  9. c++ 字符串转数字

    #字符串转整数 string ss="-99"; cout<< stoi(ss)<<endl;

  10. Spring Cloud Gateway(七):路由谓词工厂WeightRoutePredicateFactory

    本文基于 spring cloud gateway 2.0.1 接上文 5.基于路由权重(weigth)的谓词工厂 Spring Cloud Gateway 提供了基于路由权重的断言工厂,配置时指定分 ...