定义FileStream类的操作类:操作类名: FtpUpDown

上传文件

/// <summary>

/// 上传文件

/// </summary>

/// <param name="localpath">上传文件的全路径 例@"D:\123.txt"</param>

/// <param name="ftppath"></param>

/// <returns></returns>

public bool Upload(string localpath, string ftppath)

{

bool bol = false;

try

{

FileInfo fileInf = new FileInfo(localpath);

//替换符号

ftppath = ftppath.Replace("\\", "/");

//组合ftp上传文件路径

string uri = "ftp://" + ftppath + "/" + fileInf.Name;

// 根据uri创建FtpWebRequest对象

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

// 指定数据传输类型

reqFTP.UseBinary = true;

// ftp用户名和密码

reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

// 默认为true,连接不会被关闭

// 在一个命令之后被执行

reqFTP.KeepAlive = false;

// 指定执行什么命令

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

// 上传文件时通知服务器文件的大小

reqFTP.ContentLength = fileInf.Length;

// 缓冲大小设置为kb

int buffLength = 2048;

byte[] buff = new byte[buffLength];

int contentLen;

// 打开一个文件流(System.IO.FileStream) 去读上传的文件

FileStream fs = fileInf.OpenRead();

try

{

// 把上传的文件写入流

Stream strm = reqFTP.GetRequestStream();

// 每次读文件流的kb

contentLen = fs.Read(buff, 0, buffLength);

// 流内容没有结束

while (contentLen != 0)

{

// 把内容从file stream 写入upload stream

strm.Write(buff, 0, contentLen);

contentLen = fs.Read(buff, 0, buffLength);

bol = true;

}

// 关闭两个流

strm.Close();

fs.Close();

}

catch (Exception ex)

{

MessageBox.Show("上传文件失败,失败原因;" + ex.Message);

}

}

catch (Exception ex)

{

MessageBox.Show("上传文件失败,失败原因;" + ex.Message);

}

return bol;

}

下载文件:

/// <summary>

/// 下载文件

/// </summary>

/// <param name="localpath"></param>

/// <param name="fileName"></param>

/// <param name="errorinfo"></param>

/// <returns></returns>

public bool Download(string localpath, string fileName, out string errorinfo)

{

try

{

String onlyFileName = Path.GetFileName(fileName);

string newFileName = localpath + "\\" + onlyFileName;

if (File.Exists(newFileName))

{

errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);

return false;

}

string url = "ftp://" + ftpServerIP + "/FileInfo/" + fileName;

// 根据uri创建FtpWebRequest对象

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));

// 指定数据传输类型

reqFTP.UseBinary = true;

// ftp用户名和密码

reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

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);

FileStream outputStream = new FileStream(newFileName, FileMode.Create, FileAccess.Write);

while (readCount > 0)

{

outputStream.Write(buffer, 0, readCount);

readCount = ftpStream.Read(buffer, 0, bufferSize);

}

ftpStream.Close();

outputStream.Close();

response.Close();

errorinfo = "";

return true;

}

catch (Exception ex)

{

errorinfo = string.Format("因{0},无法下载", ex.Message);

return false;

}

}

调用方法:

/// <summary>

/// 上传

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Button_Click_1(object sender, RoutedEventArgs e)

{

FtpUpDown ftp = new FtpUpDown(ftpServerIP, ftpUserID, ftpPassword);

string localpath = @"D:\123.txt";

string ftppath =  ftpServerIP + @"\FileInfo";

bool bol = ftp.Upload(localpath, ftppath);

if (bol == true)

MessageBox.Show("上传成功");

else

MessageBox.Show("上传失败");

}

/// <summary>

/// 下载

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Button_Click_2(object sender, RoutedEventArgs e)

{

string errorinfo;

string localpath = @"E:\qzq\FileInfo";

if (!Directory.Exists(localpath))

{

Directory.CreateDirectory(localpath);

}

string filename = "123.txt";

if (!File.Exists(localpath + "\\" + filename))

{

FtpUpDown ftp = new FtpUpDown(ftpServerIP, ftpUserID, ftpPassword);

bool bol = ftp.Download(localpath, filename, out errorinfo);

if (bol == true)

MessageBox.Show("下载成功");

else

MessageBox.Show("下载失败:" + errorinfo + "");

}

else

{

MessageBox.Show("下载文件已存在!");

}

}

其中: ftpServerIP:上传服务的IP地址。

ftpUserID: 上传服务的登录名。

ftpPassword: 上传服务的密码。

C# FileStream进行FTP服务上传文件和下载文件的更多相关文章

  1. C# WebClient进行FTP服务上传文件和下载文件

    定义WebClient使用的操作类: 操作类名称WebUpDown WebClient上传文件至Ftp服务: //// <summary> /// WebClient上传文件至Ftp服务 ...

  2. HCNA管理设置文件系统FTP服务上传下载文件

    1.拓扑图 2.R2配置 The device is running! ###################################### <Huawei>sys Enter s ...

  3. ftp修改上传后目录、文件权限问题 aix

    问题:在AIX操作系统中,用root用户ftp文件到AIX上后,文件的默认权限是rw-r-----,Oracle用户无法读取.有没有办法指定上传文件的权限呢? 环境: AIX 6.1 解决方法: 1. ...

  4. Xshell 本地上传、远程下载文件

    1.Xshell登录工具在创建会话的时候,点击最下面的ZMODEM,可以填写下载的路径和加载的路径:2个路径可以一样也可以不一样: 在下载的时候可以下载到相应的路径去.(我设置的是下载前始终询问) 2 ...

  5. shell通过ftp实现上传/下载文件

    直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...

  6. Selenium(十一):设置元素等待、上传文件、下载文件

    1. 设置元素等待 前面我们接触了几个元素等待方法,sleep.implicitly_wait方法,这一章我们就来整体学一下. 现在大多数Web应用程序使用的都是AJAX技术.当浏览器加载页面时,页面 ...

  7. 20160113006 asp.net实现ftp上传代码(解决大文件上传问题)

    using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using S ...

  8. Linux中ftp不能上传文件/目录的解决办法

    在linux中不能上传文件或文件夹最多的问题就是权限问题,但有时也不一定是权限问题了,像我就是空间不够用了,下面我来总结一些ftp不能上传文件/目录的解决办法   在排除用户组和权限等问题后,最可能引 ...

  9. c# ftp 上传文件 与 下载文件

    接着上一篇说. 上一篇说了根据配置文件获取路径,并判断路径在服务器中是否存在.如果不存在则在服务器中建立一个. 然后就是往路径下面传输文件了.. 代码: //连接ftp private void Co ...

随机推荐

  1. Java pdf转String 并修正格式

    在尝试pdf转成String的时候,首先用python的pdfminer和pdfminer3k去尝试转换,然后资料看不太懂,就尝试用了java, 以下是java的pdfbox写的pdf转String函 ...

  2. ORA-01410: 无效的 ROWID

    视图查询单表是有这个东西的,但是视图的SQL涉及多表关联,就没这个rowid了,要么自己写个,要么不用这个ROWID做啥动作

  3. JDK中的SimpleDateFormat线程非安全

    在JDK中使用SimpleDateFormat的时候都会遇到线程安全的问题,在JDK文档中也说明了该类是线程非安全的,建议对于每个线程都创建一个SimpleDateFormat对象.如下面一个Case ...

  4. Valid Parentheses - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...

  5. 【Cf Edu #47 G】Allowed Letters

    这个题大概就是每一个位置都有一个能填字符的限制(一个点集),给出已有的$n$个字符,问能填出的最小字典序的字符串. 总体思路是贪心,每一位尽量选最小的字符. 关键在于判断在某位选了一个字符后,接下来的 ...

  6. 20165218 《网络对抗技术》Exp1 逆向及Bof基础

    Exp1 逆向及Bof基础 基础知识 1. NOP, JNE, JE, JMP, CMP汇编指令的机器码 指令 机器码 NOP NOP指令即"空指令",在x86的CPU中机器码为0 ...

  7. 20165218 《网络对抗技术》Exp0 Kali安装 Week1

    Kali Linux安装 下载 在Kali官网下载Kali Linux 64 Bit版本 打开VM,选择文件->新建虚拟机,一直点击下一步,注意这里选择稍后安装操作系统 版本中找不到Kali,可 ...

  8. tp5.1 redis 使用

    第一步:在框架根目录config里面新建redis.php文件配置ip及端口:如下: <?php return [ 'host' => '140.143.190.248', 'port' ...

  9. spring-data-redis,jedis和redis主从集成和遇到的问题

    Redis主从加哨兵的部署详见http://www.cnblogs.com/dupang/p/6414365.html spring-data-redis和jedis集成代码总体结构 代码地址http ...

  10. [Luogu 2169] 正则表达式

    [Luogu 2169] 正则表达式 感谢 0xis 推题. 记忆中很久没有过一遍写过一题了- 别被题目名称蒙骗!这不是正则表达式题目!和字符(串)处理一点关系都没有!这是个图论题啊喂! 题都没急,C ...