工具类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net; namespace CommonUtils
{
public static class FtpUtil
{
public static bool Download(string localFileFullName, Uri ftpFileFullName, string FtpUid, string FtpPwd, out string errMsg)
{
errMsg = ""; FtpWebRequest reqFTP;
FileStream outputStream = null;
try
{
outputStream = new FileStream(localFileFullName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpFileFullName);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUid, FtpPwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
} ftpStream.Close();
outputStream.Close();
response.Close(); }
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
finally
{
if (outputStream != null)
outputStream.Close();
} return true; } public static bool UploadFile(string localFile, Uri ur, string FtpUid, string FtpPwd,int timeOut, out string errMsg)
{
errMsg = ""; FileInfo fi = new FileInfo(localFile);
FileStream fs = fi.OpenRead();
long length = fs.Length;
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ur);
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.KeepAlive = false;
req.UseBinary = true;
req.ContentLength = length;
req.Timeout = timeOut * 1000;
req.ReadWriteTimeout= timeOut * 1000; try
{
Stream stream = req.GetRequestStream(); int BufferLength = 2048; //2K
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
stream.Close();
stream.Dispose(); }
catch (Exception ex)
{
errMsg = ex.Message;
return false;
} return true;
} }
}

调用:

string FtpUid = "ftpcer";
string FtpPwd = "toda";
string errMsg = "";
string upFileFullName = lblPath.Text;//本地要上传的文件
string fileName = Path.GetFileName(upFileFullName); string _o2oFtpBaseUrl = "ftp://127.0.0.1:23";
string ftpFullName = _o2oFtpBaseUrl + "/" + fileName;
Uri ur = new Uri(ftpFullName);//远程FTP全路径名 bool bRst = CommonUtils.FtpUtil.UploadFile(upFileFullName, ur, FtpUid, FtpPwd,30, out errMsg);
if (bRst)
{
MessageBox.Show("上传成功!");
}
else
{
MessageBox.Show("上传失败!" + errMsg);
}

--

C#.NET 操作FTP的更多相关文章

  1. 使用python操作FTP上传和下载

    函数释义 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下 ftp登陆连接 from ftplib import F ...

  2. C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法

    最近在做项目的时候需要操作ftp进行文件的上传下载,但在调用using (var response = (FtpWebResponse)FtpWebRequest.GetResponse())的时候总 ...

  3. Asp.Net操作FTP方法

    将用户上传的附件(文件.图片等)通过FTP方式传送到另外一台服务器上,从而缓解服务器压力 1.相关的文章如下: Discuz!NT中远程附件的功能实现[FTP协议] http://www.cnblog ...

  4. java操作FTP的一些工具方法

    java操作FTP还是很方便的,有多种开源支持,这里在apache开源的基础上自己进行了一些设计,使用起来更顺手和快捷. 思路: 1.设计FTPHandler接口,可以对ftp,sftp进行统一操作, ...

  5. 【转】 C#操作FTP

    代码不要忘记引入命名空间using System.Net;using System.IO;下面的几个步骤包括了使用FtpWebRequest类实现ftp功能的一般过程1.创建一个FtpWebReque ...

  6. C# 操作FTP

    操作FTP管理类: using System; using System.Collections.Generic; using System.Text; using System.Net; using ...

  7. ftp客户端自动同步 Windows系统简单操作ftp客户端自动同步

    服务器管理工具它是一款功能强大的服务器集成管理器,包含win系统和linux系统的批量连接,vnc客户端,ftp客户端等等实用功能.我们可以使用这款软件的ftp客户端定时上传下载的功能来进实现ftp客 ...

  8. PHP操作FTP类 (上传下载移动创建等)

    使用PHP操作FTP-用法 Php代码 收藏代码 <? // 联接FTP服务器 $conn = ftp_connect(ftp.server.com); // 使用username和passwo ...

  9. C#使用Sockets操作FTP【转载】

    using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; ...

  10. python下操作ftp上传

    生产情况:tomcat下业务log备份,目录分多级,然后对应目录格式放到ftp上:所以,结构上 我就是一级一级目录进行判断(因为我没有找到在ftp一次判断其子目录是否存在),还有一个low点就是我没有 ...

随机推荐

  1. TairSearch:加速多列索引查询

    简介: 互联网及传统行业应用服务的关键数据一般存储在MySQL这类的关系型数据库中.如需缓解数据库访问压力,可引入Redis等缓存系统承担热数据的查询,以此提升查询效能.然而业务场景如果是在数据库上做 ...

  2. Java编程技巧之样板代码

    简介: 在日常编码的过程中,可以总结出很多"样板代码",就像"活字印刷术中的"活字"一样.当我们编写新的代码时,需要用到这些"活字" ...

  3. 云原生 DevOps,模型化应用交付能力很重要!

    ​简介: DevOps 文化及其支撑其落地实践的自动化工具与平台能力在云原生架构渐为普及的背后,发挥了关键的价值. 撰稿:溪洋 云原生正在成为企业业务创新和解决规模化挑战的加速器. 云原生带来的变革绝 ...

  4. [ML] Google colab GPU 免费使用, 可挂载 Google drive

    colab 的文本行就相当于命令行,命令统一都在前面加 ! . 开启 GPU 加速,通过菜单栏的 "修改" 菜单,选择 "笔记本设置". 挂载 Google d ...

  5. WPF 从零自己实现从 RealTimeStylus 获取触摸信息

    本文将告诉大家什么是 RealTimeStylus 以及如何从零开始不使用 WPF 框架提供的功能从 RealTimeStylus 获取到触摸信息 开始之前先复习一下 Windows 的触摸演进.在上 ...

  6. aspnetcore两种上传图片(文件)的方式

    aspnetcore上传图片也就是上传文件有两种方式,一种是通过form-data,一种是binary. 先介绍第一种form-data: 该方式需要显示指定一个IFormFile类型,该组件会动态通 ...

  7. 如何将本地项目第一次同步到gitee远程

    一,Gitee账号的注册/登录 在gitee登录入口输入相关信息进行注册登录https://gitee.com/signup#lang=zh-CN 二,本地安装git客户端并配置用户信息 1.Git ...

  8. sendmail发送慢的问题

    1.使用python的脚本,发送邮件.代码如下: 点击查看代码 [root@ZabbixServerMasterNode ~]# cd /etc/zabbix/alertscripts/ [root@ ...

  9. 大营销抽奖系统,DDD开发要如何建模?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 大家好,我是技术UP主小傅哥. ‍ 经过5.1假期的一顿框框输出,终于完成了<大营销项目 ...

  10. WEB服务与NGINX(12)-NGINX的变量

    目录 1. nginx的变量 1.1 内置变量 1.2 自定义变量 1. nginx的变量 nginx的变量可以在配置文件中引用,作为功能判断或日志等场景使用,变量可以分为内置变量和自定义变量. 内置 ...