using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace PrinterDemo
{
class FtpDemo
{
/// <summary>
/// ftp有两种使用模式:主动和被动;
/// 主动模式要求客户端和服务器同时打开并且监听一个端口以建立连接;这种情况下,由于防火墙的存在,会出现一些问题;
/// 被动模式只要求服务器端产生一个监听相应端口的进程,这样就可以绕过防火墙。
/// <summary>
/// 上传数据到ftp服务器
/// </summary>
/// <param name="ftpServerIP">FTP服务器的IP地址</param>
/// <param name="ftpUserID">FTP服务器的登录用户名</param>
/// <param name="ftpPassword">FTP服务器的登录密码</param>
/// <returns></returns>
public static int UploadFtp(string ftpServerIP, string ftpUserID, string ftpPassword)
{
// 需要上传到FTP服务器的本地文件
FileInfo fileInfo = new FileInfo("D://temp//data.txt");
// 用于接收上传文件的FTP服务器上的文件地址
string uri = "ftp://" + ftpServerIP + "/data.txt";
// 实例化FTP服务器连接对象,创建连接
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
try
{
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 设置FTP服务器的用户名和密码
reqFTP.KeepAlive = false; // 执行完命令之后关闭连接
reqFTP.UseBinary = true; // 使用二进制传输方式
reqFTP.EnableSsl = false; // 不使用SSL连接
reqFTP.UsePassive = true; // 使用被动模式
// 指定发送到FTP服务器的命令为:上传
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
int buffLength = 1024;
byte[] buff = new byte[buffLength];
// 读取本地文件数据流
FileStream fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
int contentLen = fs.Read(buff, 0, buffLength);
// 通过FTP服务器上传数据流将本地文件数据写入到服务器上
Stream strm = reqFTP.GetRequestStream();
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭流
strm.Close();
fs.Close();
return 0;
}
catch (Exception ex)
{
return -1;
}
}
/// <summary>
/// ftp方式下载(从FTP服务器上指定的文件中下载数据)
/// </summary>
/// <returns></returns>
public static int DownloadFtp(string ftpServerIP, string ftpUserID, string ftpPassword)
{
// 本地文件,用于接收从FTP服务器下载的数据
FileStream outputStream = new FileStream("D://temp//hello.txt", FileMode.OpenOrCreate);
try
{
// 指定服务器上要下载的文件路径,创建一个FTP请求,建立连接
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/data.txt"));
// 指定ftp服务器的登录用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UseBinary = true; // 使用二进制传输方式
reqFTP.KeepAlive = false; // 命令执行之后关闭连接
reqFTP.EnableSsl = false; // 不使用SSL连接
reqFTP.UsePassive = true; // 使用被动模式
// 设置要发送到FTP服务器的命令为:下载
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
// 获取FTP服务器响应的数据流
Stream ftpStream = reqFTP.GetResponse().GetResponseStream();
// 读取FTP服务器上指定文件的数据
byte[] buffer = new byte[1024];
int readCount = ftpStream.Read(buffer, 0, 1024);
while (readCount > 0)
{
// 将从FTP服务器上读取的数据写入本地文件中
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, readCount);
}
ftpStream.Close();
outputStream.Close();
return 0;
}
catch (Exception ex)
{
return -1;
}
}
// 遍历FTP服务器根目录下所有的文件,并下载指定的文件
public static void FindAllFileFromFtp(string ftpServerIP, string ftpUserID, string ftpPassword)
{
string sfz = "370820199507215311";
string date = DateTime.Now.ToString("yyyyMMdd");
try
{
// 创建一个FTP请求,建立连接
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP));
// 指定ftp服务器的登录用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UseBinary = true; // 使用二进制传输方式
reqFTP.KeepAlive = false; // 命令执行之后关闭连接
reqFTP.EnableSsl = false; // 不使用SSL连接
reqFTP.UsePassive = true; // 使用被动模式
// 设置要发送到FTP服务器的命令为:获取服务器上的文件列表
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
// 获取FTP服务器的相应数据流
Stream stream = reqFTP.GetResponse().GetResponseStream();
// 根据FTP服务器的相应数据流实例化一个 读入流
StreamReader reader = new StreamReader(stream, Encoding.Default);
String fileName = reader.ReadLine(); // 读取ftp服务器上的一行数据
while (fileName != null)
{
// fileName = "JS201700000082_370820199507215311_20170802.zip";
if (fileName.EndsWith(".zip"))
{
String[] fileNames = fileName.Split('_');
if (fileNames[1].Equals(sfz) && fileNames[2].Equals(date + ".zip"))
{
// 下载指定的文件
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
Stream ftpStream = reqFTP.GetResponse().GetResponseStream();
byte[] buffer = new byte[1024];
// 实例化本地文件的输入流,用于接收从FTP服务器下载的数据
FileStream outputStream = new FileStream("D://temp//" + fileName, FileMode.OpenOrCreate);
int readCount = ftpStream.Read(buffer, 0, 1024);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, 1024);
}
ftpStream.Close();
outputStream.Close();
return;
}
}
fileName = reader.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
C#学习笔记之从FTP服务器上传和下载数据(二)
最新推荐文章于 2023-02-15 10:04:38 发布