[C#]工具类—FTP上传下载
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上传下载的更多相关文章
- FastDFSClient工具类 文件上传下载
package cn.itcast.fastdfs.cliennt; import org.csource.common.NameValuePair; import org.csource.fastd ...
- 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)
前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...
- ftp上传下载工具类
package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...
- FTP上传下载工具(FlashFXP) v5.5.0 中文版
软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...
- Java.ftp上传下载
1:jar的maven的引用: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- java客户端调用ftp上传下载文件
1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...
- 使用cmd命令行方式登录ftp上传下载数据
部分用户在使用ftp工具登录空间上传下载过程中经常会遇到各种问题,如主动模式,被动模式,以及其他导致无法登陆ftp .上传数据.下载数据的问题,这时候不妨使用一下命令行方式.命令行下可以避免很多由于f ...
- windows系统下ftp上传下载和一些常用命令
先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...
随机推荐
- ThinkPHP中视图模型详解.
很多TP的新手对于模型中的视图模型不甚了解,官方虽然有详细手册,但是对于初学者来说还是比较难以理解! 先简单说一下视图模型所能实现的功能,基本就是主表与副表之间各个字段的关联问题,实现多表关联查询,相 ...
- [状压dp]POJ2686 Traveling by Stagecoach
题意: m个城市, n张车票, 每张车票$t_i$匹马, 每张车票可以沿某条道路到相邻城市, 花费是路的长度除以马的数量. 求a到b的最小花费, 不能到达输出Impossible $1\le n\le ...
- Android 性能优化之使用MAT分析内存泄露问题
我们平常在开发Android应用程序的时候,稍有不慎就有可能产生OOM,虽然JAVA有垃圾回收机,但也不能杜绝内存泄露,内存溢出等问题,随着科技的进步,移动设备的内存也越来越大了,但由于Android ...
- 【HDOJ】1408 盐水的故事
简单题,感觉非常简单,像小学奥数的植树问题. #include <stdio.h> #include <math.h> #define MAXNUM 5001 int main ...
- WCF - Overview
WCF stands for Windows Communication Foundation. The elementary feature of WCF is interoperability. ...
- wcf教程
WCF Tutorial WCF stands for Windows Communication Foundation. It is a framework for building, config ...
- 函数page_cur_search_with_match
/****************************************************************//** Searches the right position fo ...
- Webform——购物车
购物车主要实现的功能: ①在主页面可以将所有商品显示出来,包括价格,库存. ②点击购买可以累加产品,如果是同一种产品,只会累加每种产品的数量. ③查看购物车,可以查看明细,包括所购物品的名称,价格,数 ...
- .NET(C#)调用webService获取客户端IP地址所属区域(非异步)
功能描述: 此接口用于获取客户端访问的IP的地址所属的区域(国家,城市等).通过输入IP地址查询国家.城市.所有者等信息.没有注明国家的为中国输入参数:IP地址(自动替换 " ." ...
- OpenXml操作Word的一些操作总结. - 天天不在
OpenXml相对于用MS提供的COM组件来生成WORD,有如下优势: 1.相对于MS 的COM组件,因为版本带来的不兼容问题,及各种会生成WORD半途会崩溃的问题. 2.对比填满一张30多页的WOR ...