ftp文件下载
2015-07-29 15:44
573 查看
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace GasMap.Common
{
public class FtpFile
{
private string ftpUserName;
private string ftpPassword;
/// <summary>
/// 连接服务器
/// </summary>
/// <param name="server">服务器地址(IP)</param>
/// <param name="userName">登录名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public void InitFtpParam(string userName, string pwd)
{
ftpUserName = userName;
ftpPassword = pwd;
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="ftpDir">ftp文件目录</param>
/// <param name="fileName">文件名</param>
/// <returns>文件buffer</returns>
public bool DownloadFile(string ftpDir, string fileName, string localDir)
{
FtpWebRequest reqFTP;
try
{
// 如果文件存在,则覆盖,否则创建文件
string ftpFileName = FolderFormat(ftpDir, '/') + fileName;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
//StreamReader ftpFileListReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
Stream ftpStream = response.GetResponseStream();
int len = 0;
//List<byte> bytes = new List<byte>();
int ret = -1;
FileStream outputStream = new FileStream(FolderFormat(localDir, '\\') + fileName, FileMode.Create);
while ((ret = ftpStream.ReadByte()) != -1)
{
//bytes.Add(Convert.ToByte(ret));
len++;
outputStream.WriteByte(Convert.ToByte(ret));
}
ftpStream.Close();
response.Close();
outputStream.Close();
//byte[] filebuffer = ftpStream.ToArray();
//long fileLen = ftpFileListReader.
//byte[] filebuffer = bytes.ToArray();
//ftpStream.Read(filebuffer, 0, Convert.ToInt32(len));
return true;
}
catch (Exception ex)
{
//throw ex.Message();
return false;
}
}
/// <summary>
/// 格式化目录
/// </summary>
/// <param name="folderPath">要格式化的目录</param>
/// <param name="fromatChar">格式化的目录分隔符一般为 \ / </param>
/// <returns>格式化后的目录带目录分隔符</returns>
public static string FolderFormat(string folderPath, char fromatChar)
{
int pos = -1;
pos = folderPath.LastIndexOf(fromatChar);
if (pos != folderPath.Length - 1)
{
folderPath += fromatChar;
}
return folderPath;
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="ftpDir"></param>
/// <param name="localDir"></param>
/// <param name="fileName"></param>
/// <returns></returns>
/* public bool DownloadFile(string ftpDir, string localDir, string fileName)
{
try
{
// 获得文件buffer
byte[] fileBuffer = DownloadFile(ftpDir, fileName);
if (fileBuffer.Length > 0)
{
// 如果文件存在,则覆盖,否则创建文件
FileStream outputStream = new FileStream(FolderFormat(localDir, '\\') + fileName, FileMode.Create);
int bufferSize = 2048; // 每次读2k
int readCount = bufferSize;
outputStream.Write(fileBuffer, 0, fileBuffer.Length);
//while (readCount <= fileBuffer.Length)
//{
// outputStream.Write(fileBuffer, 0, readCount);
// readCount += bufferSize;
//}
outputStream.Close();
}
return true;
}
catch (Exception ex)
{
//throw ex.Message();
return false;
}
}*/
}
}
引用:
var ftp = new FtpFile();
ftp.InitFtpParam(username, pwd);
var ftpPath = string.Format(@"ftp://{0}/{1}{2}", serverid, docname, tt.Rows[0]["PathName"].ToString().Replace("\\", "/"));
bl =
ftp.DownloadFile(ftpPath, fileName, LocalPath);
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace GasMap.Common
{
public class FtpFile
{
private string ftpUserName;
private string ftpPassword;
/// <summary>
/// 连接服务器
/// </summary>
/// <param name="server">服务器地址(IP)</param>
/// <param name="userName">登录名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public void InitFtpParam(string userName, string pwd)
{
ftpUserName = userName;
ftpPassword = pwd;
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="ftpDir">ftp文件目录</param>
/// <param name="fileName">文件名</param>
/// <returns>文件buffer</returns>
public bool DownloadFile(string ftpDir, string fileName, string localDir)
{
FtpWebRequest reqFTP;
try
{
// 如果文件存在,则覆盖,否则创建文件
string ftpFileName = FolderFormat(ftpDir, '/') + fileName;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
//StreamReader ftpFileListReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
Stream ftpStream = response.GetResponseStream();
int len = 0;
//List<byte> bytes = new List<byte>();
int ret = -1;
FileStream outputStream = new FileStream(FolderFormat(localDir, '\\') + fileName, FileMode.Create);
while ((ret = ftpStream.ReadByte()) != -1)
{
//bytes.Add(Convert.ToByte(ret));
len++;
outputStream.WriteByte(Convert.ToByte(ret));
}
ftpStream.Close();
response.Close();
outputStream.Close();
//byte[] filebuffer = ftpStream.ToArray();
//long fileLen = ftpFileListReader.
//byte[] filebuffer = bytes.ToArray();
//ftpStream.Read(filebuffer, 0, Convert.ToInt32(len));
return true;
}
catch (Exception ex)
{
//throw ex.Message();
return false;
}
}
/// <summary>
/// 格式化目录
/// </summary>
/// <param name="folderPath">要格式化的目录</param>
/// <param name="fromatChar">格式化的目录分隔符一般为 \ / </param>
/// <returns>格式化后的目录带目录分隔符</returns>
public static string FolderFormat(string folderPath, char fromatChar)
{
int pos = -1;
pos = folderPath.LastIndexOf(fromatChar);
if (pos != folderPath.Length - 1)
{
folderPath += fromatChar;
}
return folderPath;
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="ftpDir"></param>
/// <param name="localDir"></param>
/// <param name="fileName"></param>
/// <returns></returns>
/* public bool DownloadFile(string ftpDir, string localDir, string fileName)
{
try
{
// 获得文件buffer
byte[] fileBuffer = DownloadFile(ftpDir, fileName);
if (fileBuffer.Length > 0)
{
// 如果文件存在,则覆盖,否则创建文件
FileStream outputStream = new FileStream(FolderFormat(localDir, '\\') + fileName, FileMode.Create);
int bufferSize = 2048; // 每次读2k
int readCount = bufferSize;
outputStream.Write(fileBuffer, 0, fileBuffer.Length);
//while (readCount <= fileBuffer.Length)
//{
// outputStream.Write(fileBuffer, 0, readCount);
// readCount += bufferSize;
//}
outputStream.Close();
}
return true;
}
catch (Exception ex)
{
//throw ex.Message();
return false;
}
}*/
}
}
引用:
var ftp = new FtpFile();
ftp.InitFtpParam(username, pwd);
var ftpPath = string.Format(@"ftp://{0}/{1}{2}", serverid, docname, tt.Rows[0]["PathName"].ToString().Replace("\\", "/"));
bl =
ftp.DownloadFile(ftpPath, fileName, LocalPath);
相关文章推荐
- mysql 连接php中文乱码
- mysql 连接php中文乱码
- mysql 连接php中文乱码
- PHP如何实现Unicode和Utf-8编码相互转换
- EditPlus自动补全、模板配置
- PHP正则表达式
- php读取excel中的时间转换问题
- php模拟post请求 测试api接口
- PHP模拟QQ登录的方法
- PHP Fuzzing行动——源码审计 黑客注入防范
- PHP 常用正则汇总
- PHP模拟表单的post请求实现登录
- PHP实现动态web服务器方法
- php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法
- TP框架中隐藏的预处理函数,前置钩子
- php 正则表达式捕获组与非捕获组
- php简单防盗链实现方法
- 如何用PHP来实现一个动态Web服务器
- laravel5的坑
- cookie 在php中的实现