C#访问ftp地址下载jpg、pdf、⽂件夹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/// <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.Flush();
fs.Close();
responseStream.Close(); }
}
finally
{
if (fs != null)
fs.Close();
if (responseStream != null) responseStream.Close(); }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论