工具类:

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. 一文搞懂 SAE 日志采集架构

    简介: 本文将着重介绍了 SAE 提供了多种日志采集方案,以及相关的架构,场景使用特点,点击下文,立即查看吧- 作者:牛通(奇卫)   日志,对于一个程序的重要程度不言而喻.无论是作为排查问题的手段, ...

  2. 一首歌的时间,手把手搭建基于FC的网站

    简介: 不是杰伦的那一首歌啦~ 部署网站 说好不哭 在接触serverless架构之前,我们如果想实现上线一个Web网站,就要在开发前期经过操作很多冗杂但又必须的步骤,不少小白可谓是快速的从入门到退坑 ...

  3. 博时基金基于 RocketMQ 的互联网开放平台 Matrix 架构实践

    简介: Matrix 经过一年多的建设,目前已具备多渠道统一接入.第三方生态互联互通.基金特色交易场景化封装等功能特性.Matrix 通过建设有品质.有温度的陪伴,从技术上和体验上,让用户理解风险,理 ...

  4. KubeDL 加入 CNCF Sandbox,加速 AI 产业云原生化

    ​简介: 2021 年 6 月 23 日,云原生计算基金会(CNCF)宣布通过全球 TOC 投票接纳 KubeDL 成为 CNCF Sandbox 项目.KubeDL 是阿里开源的基于 Kuberne ...

  5. Serverless Devs 2.0 全新发布,让 Serverless 应用开发更简单

    ​简介: 2020 年 10 月 23日,阿里巴巴正式宣布开源其首个 Serverless 开发者平台 Serverless Devs.历经近一年精心打磨,今天 Serverless Devs 2.0 ...

  6. [Ethereum] 浅谈 ERC20 在 openzeppelin-contracts 中的结构与实现

    目前 openzeppelin-contracts 的稳定版是 v2.5,截止到本文发布,最新的 Tag 为 v3.0.0-rc.0 以下是 token/ETC20 的文件列表: IERC20.sol ...

  7. dotnet 5 让 WPF 调用 WindowsRuntime 方法

    本文告诉大家在 dotnet 5 里,如何使用 WinRT 加上 Microsoft.Windows.SDK 的辅助来调用 WindowsRuntime 方法.当前是 2021.10 此时的 Wind ...

  8. WPF 框架开发 ColumnDefinition 和 RowDefinition 的代码在哪

    我的 VisualStudio 在更新到 2022 就构建不通过 WPF 仓库,提示我在 Grid 的代码里面找不到 ColumnDefinitionCollection 和 RowDefinitio ...

  9. 利用引用传递一次遍历构造菜单树(附java&go demo)

    目录 原理讲解 java demo Go demo 优点 原理讲解 利用引用传递,当儿子的儿子变动的时候,自己的儿子的儿子也变动(取地址) java demo package com.huiyuan. ...

  10. Oracle和达梦:循环执行SQL(如循环插入数据)

    Oracle和达梦:循环执行SQL(如循环插入数据) 其中:WHILE i <= 100000 LOOP,10万是循环10万次 其中:i NUMBER := 1;,1是从一开始 -- 循环执行一 ...