您的位置:首页 > 编程语言 > PHP开发

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: