You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
276 lines
7.9 KiB
276 lines
7.9 KiB
3 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Text;
|
||
|
using System.Net;
|
||
|
using System.IO;
|
||
|
|
||
|
namespace Stone.Common
|
||
|
{
|
||
|
public class MyFTP
|
||
|
{
|
||
|
#region FTP��ȡ�ļ��б�
|
||
|
|
||
|
/// <summary>
|
||
|
/// FTP��ȡ�ļ��б�
|
||
|
/// </summary>
|
||
|
/// <param name="ftpServerIP"></param>
|
||
|
/// <param name="ftpUserID"></param>
|
||
|
/// <param name="ftpPassword"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string[] FTPGetFileList(string ftpServerIP, string ftpUserID, string ftpPassword)
|
||
|
{
|
||
|
//��Ӧ����
|
||
|
StringBuilder result = new StringBuilder();
|
||
|
|
||
|
//FTP����
|
||
|
FtpWebRequest ftpRequest = null;
|
||
|
|
||
|
//FTP��Ӧ
|
||
|
WebResponse ftpResponse = null;
|
||
|
|
||
|
//FTP��Ӧ��
|
||
|
StreamReader ftpResponsStream = null;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//����FTP����
|
||
|
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
|
||
|
|
||
|
//�����ļ���������
|
||
|
ftpRequest.UseBinary = true;
|
||
|
|
||
|
//FTP��¼
|
||
|
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
|
||
|
|
||
|
//����FTP����
|
||
|
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
|
||
|
|
||
|
//����FTP��Ӧ
|
||
|
ftpResponse = ftpRequest.GetResponse();
|
||
|
|
||
|
//FTP��Ӧ��
|
||
|
ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream());
|
||
|
|
||
|
string line = ftpResponsStream.ReadLine();
|
||
|
|
||
|
while (line != null)
|
||
|
{
|
||
|
result.Append(line);
|
||
|
result.Append("\n");
|
||
|
line = ftpResponsStream.ReadLine();
|
||
|
}
|
||
|
|
||
|
//ȥ�������б�������һ������
|
||
|
result.Remove(result.ToString().LastIndexOf('\n'), 1);
|
||
|
|
||
|
//���ؽ���
|
||
|
return result.ToString().Split('\n');
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Console.WriteLine(ex.Message);
|
||
|
return (null);
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (ftpResponsStream != null)
|
||
|
{
|
||
|
ftpResponsStream.Close();
|
||
|
}
|
||
|
|
||
|
if (ftpResponse != null)
|
||
|
{
|
||
|
ftpResponse.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region FTP�����ļ�
|
||
|
|
||
|
/// <summary>
|
||
|
/// FTP�����ļ�
|
||
|
/// </summary>
|
||
|
/// <param name="ftpServerIP">FTP������IP</param>
|
||
|
/// <param name="ftpUserID">FTP��¼�ʺ�</param>
|
||
|
/// <param name="ftpPassword">FTP��¼����</param>
|
||
|
/// <param name="saveFilePath">�����ļ�·��</param>
|
||
|
/// <param name="saveFileName">�����ļ���</param>
|
||
|
/// <param name="downloadFileName">�����ļ���</param>
|
||
|
public static void FTPDownloadFile(string ftpServerIP, string ftpUserID, string ftpPassword,
|
||
|
string saveFilePath, string saveFileName, string downloadFileName)
|
||
|
{
|
||
|
//����FTP��������
|
||
|
FtpWebRequest ftpRequest = null;
|
||
|
//����FTP��Ӧ����
|
||
|
FtpWebResponse ftpResponse = null;
|
||
|
|
||
|
//�洢��
|
||
|
FileStream saveStream = null;
|
||
|
//FTP������
|
||
|
Stream ftpStream = null;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//���������ļ�
|
||
|
saveStream = new FileStream(saveFilePath + "\\" + saveFileName, FileMode.Create);
|
||
|
|
||
|
//����FTP��������
|
||
|
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + downloadFileName));
|
||
|
|
||
|
//���������ļ�����
|
||
|
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
|
||
|
|
||
|
//�����ļ���������
|
||
|
ftpRequest.UseBinary = true;
|
||
|
|
||
|
//���õ�¼FTP�ʺź�����
|
||
|
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
|
||
|
|
||
|
//����FTP��Ӧ����
|
||
|
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||
|
|
||
|
//��ȡFTP��Ӧ������
|
||
|
ftpStream = ftpResponse.GetResponseStream();
|
||
|
|
||
|
//��Ӧ���ݳ���
|
||
|
long cl = ftpResponse.ContentLength;
|
||
|
|
||
|
int bufferSize = 2048;
|
||
|
|
||
|
int readCount;
|
||
|
|
||
|
byte[] buffer = new byte[bufferSize];
|
||
|
|
||
|
//����FTP�ļ���
|
||
|
readCount = ftpStream.Read(buffer, 0, bufferSize);
|
||
|
|
||
|
while (readCount > 0)
|
||
|
{
|
||
|
saveStream.Write(buffer, 0, readCount);
|
||
|
|
||
|
readCount = ftpStream.Read(buffer, 0, bufferSize);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Console.WriteLine(ex.Message);
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (ftpStream != null)
|
||
|
{
|
||
|
ftpStream.Close();
|
||
|
}
|
||
|
|
||
|
if (saveStream != null)
|
||
|
{
|
||
|
saveStream.Close();
|
||
|
}
|
||
|
|
||
|
if (ftpResponse != null)
|
||
|
{
|
||
|
ftpResponse.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region FTP�ϴ��ļ�
|
||
|
|
||
|
/// <summary>
|
||
|
/// FTP�ϴ��ļ�
|
||
|
/// </summary>
|
||
|
/// <param name="ftpServerIP">FTP������IP</param>
|
||
|
/// <param name="ftpUserID">FTP��¼�ʺ�</param>
|
||
|
/// <param name="ftpPassword">FTP��¼����</param>
|
||
|
/// <param name="filename">���ļ��ļ���������·����</param>
|
||
|
public static void FTPUploadFile(string ftpServerIP, string ftpUserID, string ftpPassword, string filename, string ftpPath)
|
||
|
{
|
||
|
//�ϴ��ļ�
|
||
|
FileInfo uploadFile = null;
|
||
|
|
||
|
//�ϴ��ļ���
|
||
|
FileStream uploadFileStream = null;
|
||
|
|
||
|
//FTP��������
|
||
|
FtpWebRequest ftpRequest = null;
|
||
|
|
||
|
//FTP��
|
||
|
Stream ftpStream = null;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//��ȡ�ϴ��ļ�
|
||
|
uploadFile = new FileInfo(filename);
|
||
|
|
||
|
//����FtpWebRequest����
|
||
|
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpPath + "/" + uploadFile.Name));
|
||
|
|
||
|
//FTP��¼
|
||
|
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
|
||
|
|
||
|
// Ĭ��Ϊtrue�����Ӳ��ᱻ�ر�
|
||
|
// ��һ������֮����ִ��
|
||
|
ftpRequest.KeepAlive = false;
|
||
|
|
||
|
//FTP����ִ�з���
|
||
|
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
|
||
|
|
||
|
// ָ�����ݴ�������
|
||
|
ftpRequest.UseBinary = true;
|
||
|
|
||
|
// �ϴ��ļ�ʱ֪ͨ�������ļ��Ĵ�С
|
||
|
ftpRequest.ContentLength = uploadFile.Length;
|
||
|
|
||
|
// ������С����Ϊ2kb
|
||
|
int buffLength = 2048;
|
||
|
|
||
|
byte[] buff = new byte[buffLength];
|
||
|
int contentLen;
|
||
|
|
||
|
// ����һ���ļ������ϴ����ļ�
|
||
|
uploadFileStream = uploadFile.OpenRead();
|
||
|
|
||
|
// ���ϴ����ļ�д����
|
||
|
ftpStream = ftpRequest.GetRequestStream();
|
||
|
|
||
|
// ÿ�ζ��ļ�����2kb
|
||
|
contentLen = uploadFileStream.Read(buff, 0, buffLength);
|
||
|
|
||
|
// ������û�н���
|
||
|
while (contentLen != 0)
|
||
|
{
|
||
|
// �����ݴ�file stream д�� upload stream
|
||
|
ftpStream.Write(buff, 0, contentLen);
|
||
|
|
||
|
contentLen = uploadFileStream.Read(buff, 0, buffLength);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (uploadFileStream != null)
|
||
|
{
|
||
|
uploadFileStream.Close();
|
||
|
}
|
||
|
|
||
|
if (ftpStream != null)
|
||
|
{
|
||
|
ftpStream.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
}
|
||
|
}
|