//上传文件
         public static Boolean FtpUpload(string ftpPath, string localFile, FtpServer svr)
         {
             //检查目录是否存在,不存在创建
             FtpCheckDirectoryExist(ftpPath, svr);
             FileInfo fi = new FileInfo(localFile);
             FileStream fs = fi.OpenRead();
             long length = fs.Length;
             FtpWebRequest req = (FtpWebRequest)WebRequest.Create(svr.FtpServerIp + ftpPath + fi.Name);
             req.Credentials = new NetworkCredential(svr.FtpUserId, svr.FtpPassword);
             req.Method = WebRequestMethods.Ftp.UploadFile;
             req.ContentLength = length;
             req.Timeout =  * ;
             try
             {
                 Stream stream = req.GetRequestStream();
                 ; //2K
                 byte[] b = new byte[BufferLength];
                 int i;
                 , BufferLength)) > )
                 {
                     stream.Write(b, , i);
                 }
                 stream.Close();
                 stream.Dispose();
             }
             catch (Exception)
             {
                 return false;
             }
             finally
             {
                 fs.Close();
                 req.Abort();
             }
             req.Abort();
             return true;
         }

         //判断文件的目录是否存,不存则创建
         public static void FtpCheckDirectoryExist(string destFilePath, FtpServer svr)
         {
             string fullDir = FtpParseDirectory(destFilePath);
             string[] dirs = fullDir.Split('/');
             string curDir = "/";
             ; i < dirs.Length; i++)
             {
                 string dir = dirs[i];
                 //如果是以/开始的路径,第一个为空
                 if (!string.IsNullOrEmpty(dir))
                 {
                     try
                     {
                         curDir += dir + "/";
                         FtpMakeDir(curDir, svr);
                     }
                     catch (Exception)
                     {
                         // ignored
                     }
                 }
             }
         }

         public static string FtpParseDirectory(string destFilePath)
         {
             , destFilePath.LastIndexOf("/", StringComparison.Ordinal));
         }

         //创建目录
         public static Boolean FtpMakeDir(string localFile, FtpServer svr)
         {
             FtpWebRequest req = (FtpWebRequest)WebRequest.Create(svr.FtpServerIp + localFile);
             req.Credentials = new NetworkCredential(svr.FtpUserId, svr.FtpPassword);
             req.Method = WebRequestMethods.Ftp.MakeDirectory;
             try
             {
                 FtpWebResponse response = (FtpWebResponse)req.GetResponse();
                 response.Close();
             }
             catch (Exception)
             {
                 req.Abort();
                 return false;
             }
             req.Abort();
             return true;
         }
         public class FtpServer
         {
             public string FtpServerIp { get; set; }
             public string FtpPath { get; set; }
             public string FtpUserId { get; set; }
             public string FtpPassword { get; set; }

         }

【2017002】C#FTP上传文件的更多相关文章

  1. .net FTP上传文件

    FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...

  2. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  3. FTP上传文件到服务器

    一.初始化上传控件. 1.我们这里用dropzone.js作为上传控件,下载地址http://www.dropzonejs.com/ 2.这里我们使用一个div元素作为dropzone载体. < ...

  4. 再看ftp上传文件

    前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...

  5. FTP上传文件提示550错误原因分析。

    今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...

  6. FTP 上传文件

    有时候需要通过FTP同步数据文件,除了比较稳定的IDE之外,我们程序员还可以根据实际的业务需求来开发具体的工具,具体的开发过程就不细说了,这里了解一下通过C#实现FTP上传文件到指定的地址. /// ...

  7. Java ftp 上传文件和下载文件

    今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...

  8. C# FTP上传文件至服务器代码

    C# FTP上传文件至服务器代码 /// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo ...

  9. Java ftp上传文件方法效率对比

    Java ftp上传文件方法效率对比 一.功能简介: txt文件采用ftp方式从windows传输到Linux系统: 二.ftp实现方法 (1)方法一:采用二进制流传输,设置缓冲区,速度快,50M的t ...

  10. Ftp上传文件

    package net.util.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...

随机推荐

  1. python中循环删除list和dict类型注意事项

    列表和字典在循环操作(增删)时,其长度会改变 # 删除 li = [11, 22, 33, 44, 'rock']中索引为单数的元素 # 方法一 del li[1::2] print(li) # [1 ...

  2. Java读取粘贴板内容

    package com.test.jvm.oom.design; import java.awt.Image; import java.awt.Toolkit; import java.awt.dat ...

  3. My SQL查询语言

    基础查询 一.语法select 查询列表from 表名;二.特点1.查询列表可以是字段.常量.表达式.函数,也可以是多个2.查询结果是一个虚拟表 三.示例1.查询单个字段select 字段名 from ...

  4. bat中实现代码拷贝到指定目录后启动命令行并更改默认路径

      ### window书写shell脚本,实现判断指定文件是否存在,存在就删除,然后复制新文件到此目录 ``` if exist "G:\test\test2\1.txt" (d ...

  5. Python-正则表达式实现计算器功能

    需求: 用户输入运算表达式,终端显示计算结果 源代码: # !/usr/bin/env/ python3 # -*- coding: utf-8 -*- """用户输入计 ...

  6. OpenLayers介绍和第一个例子(转载)

    什么是OpenLayers? 作者:田念明出处:http://www.cnblogs.com/nianming/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位 ...

  7. 05_dubbo_aop

    [对这行代码进行源码分析] ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.c ...

  8. 孙鑫VC++视频教程(1-20课全)

    孙鑫VC++视频教程(1-20课全)PPT讲义和源代码 http://down.51cto.com/data/467760 孙鑫VC++从入门到精通开发详解视频教程[20讲] http://down. ...

  9. Ganglia安装

    一.rrdtool安装 1.1 安装依赖包 由于rrdtool依赖的包比较多,而且包之间也存在依赖,故使用yum安装由于服务器无法联网,故使用iso文件创建本地yum源,方法见下: (1)创建iso存 ...

  10. Element和vue框架报错提示

    上面报错提示Error in render function: "TypeError:Cannot read property '$options' of undefined" 就 ...