public class FtpHelper
{
/// <summary>
/// ftp方式上传
/// </summary>
public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
try
{
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false; // Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // Specify the data transfer type.
reqFTP.UseBinary = true; // Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length; // The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen; // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
//FileStream fs = fileInf.OpenRead();
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream(); // Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength); // Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
} // Close the file stream and the Request Stream
strm.Close();
fs.Close();
return 0;
}
catch (Exception ex)
{
reqFTP.Abort();
// Logging.WriteError(ex.Message + ex.StackTrace);
return -2;
}
} /// <summary>
/// ftp方式下载
/// </summary>
public static int DownloadFtp(string filePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FtpWebRequest reqFTP;
try
{
//filePath = < <The full path where the file is to be created.>>,
//fileName = < <Name of the file to be created(Need not be the name of the file on FTP server).>>
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
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);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
} ftpStream.Close();
outputStream.Close();
response.Close();
return 0;
}
catch (Exception ex)
{
// Logging.WriteError(ex.Message + ex.StackTrace);
// System.Windows.Forms.MessageBox.Show(ex.Message);
return -2;
}
}
}

  不错的文章:http://www.cnblogs.com/greatverve/archive/2012/03/03/csharp-ftp.html

[C#]工具类—FTP上传下载的更多相关文章

  1. FastDFSClient工具类 文件上传下载

    package cn.itcast.fastdfs.cliennt; import org.csource.common.NameValuePair; import org.csource.fastd ...

  2. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

  3. ftp上传下载工具类

    package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...

  4. FTP上传下载工具(FlashFXP) v5.5.0 中文版

    软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...

  5. Java.ftp上传下载

    1:jar的maven的引用: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  6. java客户端调用ftp上传下载文件

    1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...

  7. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  8. 使用cmd命令行方式登录ftp上传下载数据

    部分用户在使用ftp工具登录空间上传下载过程中经常会遇到各种问题,如主动模式,被动模式,以及其他导致无法登陆ftp .上传数据.下载数据的问题,这时候不妨使用一下命令行方式.命令行下可以避免很多由于f ...

  9. windows系统下ftp上传下载和一些常用命令

    先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...

随机推荐

  1. responsive web design

    http://d.alistapart.com/responsive-web-design/ex/ex-site-flexible.html http://alistapart.com/article ...

  2. Java泛型反射机制(二)

    /** * @author Administrator * 好处:泛型:1安全 2减少代码重用率 */ package com.test; import java.lang.reflect.Metho ...

  3. Altium Designer完美双屏显示方法演示

    布线时我们往往需要对一些信号线做特别的走线处理,这样需要边布线边对照原理图,在protel99中那是一个很痛苦的事,在Altium Designer中这种情况将变很简单. 硬件要求,笔记本+外接显示器 ...

  4. 驱动开发 - WDK 调试及 SVN 环境搭建

    由于从公司辞职了,所以以前在公司里搭建的驱动开发环境也就 Game Over 了, 同样由于那环境是很久以前搭建的,自己也有很多记不清楚的地方了, 而且其中还是有很多需要注意的地方的,所以在这里顺便做 ...

  5. WPF中的MatrixTransform

    原文:WPF中的MatrixTransform WPF中的MatrixTransform                                                         ...

  6. MapReduce——计算温度最大值 (基于全新2.2.0API)

    MapReduce——计算温度最大值 (基于全新2.2.0API) deprecated: Job类的所有Constructors, 新的API用静态方法getInstance(conf)来去的Job ...

  7. ie调试器

    最大化影响调试的,点右上角的固定按妞

  8. [HDU 1565+1569] 方格取数

    HDU 1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. EPUB弹出窗口式脚注

    网上搜到一些国学典籍的EPUB版,虽有古人的注解,但正文和注解混排在一起,当我只想迅速读正文的时候比较碍眼.于是研究了一下 EPUB3 中有关脚注(footnote)的规格定义,写了一个 Python ...

  10. CSS clip:rect矩形剪裁功能及一些应用介绍

    CSS clip:rect矩形剪裁功能及一些应用介绍 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.co ...