1.无密码访问ftp地址下载jpg、pdf、文件夹
/// <summary>
/// 无密码访问ftp地址
/// </summary>
/// <param name="ftpPath">ftp://196.168.1.1/文件名字.jpg/.pdf等</param>
/// <param name="filePath">D:\\新建文件夹\\</param>
/// <param name="fileName">文件名字.jpg</param>
public void Download01(string ftpPath, string filePath, string fileName)
{
string StrCG = "下载成功!";
//文件传输协议
FtpWebRequest RequestFTP;
try
{
//创建流文件
FileStream CreateStream = new FileStream(filePath + fileName, FileMode.Create);
//根据URI创建FtpWebRequest对象
RequestFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
//设置请求的方法是FTP文件下载
RequestFTP.Method = WebRequestMethods.Ftp.DownloadFile;
//指定数据传输类型
RequestFTP.UseBinary = true;
RequestFTP.UsePassive = false;
//获取一个请求响应对象
FtpWebResponse ResponseXY = (FtpWebResponse)RequestFTP.GetResponse();
//把从ftp下载的文件写入流
Stream ftpStream = ResponseXY.GetResponseStream();
long cl = ResponseXY.ContentLength;
//缓冲文件大小设置为2kb
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
//每次读取文件流的2kb
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
//把内容从文件流写入
CreateStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();//把从ftp下载的文件写入流关闭
CreateStream.Close();//把内容从文件流写入关闭
ResponseXY.Close();//ftp连接关闭
}
catch (Exception ex)
{
LogInfo("报错:" + ex);
StrCG = ex.Message;
}
}
2.有密码访问ftp地址下载jpg、pdf、文件夹
/// <summary>
/// 有密码访问ftp地址
/// </summary>
/// <param name="ftpfilepath">ftp://196.168.1.1/文件名字.jpg/.pdf等</param>
/// <param name="filePath">D:\\新建文件夹\\</param>
/// <param name="fileName">文件名字.jpg</param>
/// <param name="FTPUSERNAME">用户名字:123</param>
/// <param name="FTPPASSWORD">用户密码:123</param>
/// <returns></returns>
public bool Download02(string ftpfilepath, string filePath, string fileName, string FTPUSERNAME = "", string FTPPASSWORD = "")
{
try
{
filePath = filePath.Replace("我的电脑\\", "");
LogInfo("filePath" + filePath);
//返回指定路径的文件名和扩展名。eg:
String onlyFileName = Path.GetFileName(fileName);
string newFileName = filePath + onlyFileName;
if (File.Exists(newFileName))
{
//如果本地存在这个文件删除
File.Delete(newFileName);
}
ftpfilepath = ftpfilepath.Replace("\\", "/");
string url = ftpfilepath;
//创建一个与FTP服务器联系的FtpWebRequest对象
FtpWebRequest RequestFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
//指定数据传输类型
RequestFtp.UseBinary = true;
连接登录FTP服务器
RequestFtp.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);
//获取一个请求响应对象
FtpWebResponse ResponseFtp = (FtpWebResponse)RequestFtp.GetResponse();
//把从ftp下载的文件写入流
Stream ftpStream = ResponseFtp.GetResponseStream();
long cl = ResponseFtp.ContentLength;
//缓冲文件大小设置为2kb
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
//创建一个文件流
FileStream outputStream = new FileStream(newFileName, FileMode.Create);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
ResponseFtp.Close();
return true;
}
catch (Exception ex)
{
LogInfo("报错原因:" + ex);
return false;
}
}
3.文件夹下载(没有试过不行的话从新查个吧)
/// <summary>
/// 下载ftp文件
/// </summary>
/// <param name="ftpURI">ftp地址eg:ftp://196.168.1.1/新建文件夹</param>
/// <param name="filepath">本地存放目录</param>
/// <param name="ftpUserID">用户名</param>
/// <param name="ftpPassword">密码</param>
public void DownLoadFileFromFTP(string ftpURI, string filepath, string ftpUserID="",string ftpPassword="")
{
string LocalFileExistsOperation = "write";
Uri uri = new Uri(ftpURI);
string FileName = Path.GetFullPath(ftpURI) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath);
//创建一个文件流
FileStream fs = null;
Stream responseStream = null;
try
{
//创建一个与FTP服务器联系的FtpWebRequest对象
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
//设置请求的方法是FTP文件下载
request.Method = WebRequestMethods.Ftp.DownloadFile;
//连接登录FTP服务器
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
//获取一个请求响应对象
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//获取请求的响应流
responseStream = response.GetResponseStream();
//判断本地文件是否存在,如果存在,则打开和重写本地文件
if (File.Exists(FileName))
{
if (LocalFileExistsOperation == "write")
{
fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);
}
}
//判断本地文件是否存在,如果不存在,则创建本地文件
else
{
fs = File.Create(FileName);
}
if (fs != null)
{
int buffer_count = 65536;
byte[] buffer = new byte[buffer_count];
int size = 0;
while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
{
fs.Write(buffer, 0, size);
}
fs.Flush();
fs.Close();
responseStream.Close();
}
}
finally
{
if (fs != null)
fs.Close();
if (responseStream != null)
responseStream.Close();
}
}