ftp文件下载
来源:互联网 发布:opencv java api 中文 编辑:程序博客网 时间:2024/06/05 10:37
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);
- ftp文件下载、上传
- 使用FTP下载文件
- ftp上传 下载文件
- .net下载ftp文件
- FTP文件下载
- FTP文件下载
- 远程下载文件 FTP
- ftp 下载文件
- mfc ftp文件下载
- C# ftp 下载文件
- ftp下载文件
- 文件下载 ftp
- FTP文件的下载
- 从ftp下载文件
- C#FTP下载文件
- 【Linux】FTP文件下载
- FTP多文件下载
- ftp文件下载
- 如何在ECSHOP后台左侧导航中增加新菜单
- 在开发过程中调试报表插件详细教程
- mysql character set , collation
- 在Windows上使用Linux命令工具:CygwinPortable
- IDEA 分享项目到 Git
- ftp文件下载
- 某P2P开发商ERP系统核心业务介绍
- iOS开发使用Unwind Segue进行返回
- Session 设置有效期
- OO六大设计原则总结(上层码农必会)
- BaseActivity的重要性
- 浅析JAVA注解
- MyBasketball 第二篇 抓取网页内容
- PAT (Advanced Level) 1073. Scientific Notation (20) 科学计数法