using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Configuration;
///
/// FtpWeb 的摘要说明
/// .net对FTP操作类
///
namespace inter
{
public class FtpWebMb
{
public FtpWebMb()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private string ftpServerIP = ConfigurationManager.AppSettings["FtpServer"].ToString();
private string ftpUserID = ConfigurationManager.AppSettings["FtpUserName"].ToString();
private string ftpPassword = ConfigurationManager.AppSettings["FtpPassWord"].ToString();
private string ftpInPath = ConfigurationManager.AppSettings["FtpMbInFile"].ToString();
private string ftpOutPath = ConfigurationManager.AppSettings["FtpMbOutFile"].ToString();
public bool Upload(string filename)
{
bool Result = false;
try
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + ftpInPath + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
Result = true;
}
catch (Exception Err)
{
DataTransLog("失败", filename + "文件上传出错:" + Err.Message, "FtpWeb.Upload," + filename);
}
return Result;
}
public void DataTransLog(string Flag, string Content, string Other)
{
IniFile ls_IniFile = new IniFile(Environment.CurrentDirectory + "\\log.ini");
ls_IniFile.WriteLog("ReadData1", Content);
/* string Sql = "Insert into DataTrans_Log (DTLOG01,DTLOG02,DTLOG03,DTLOG04)"
+ " Values (getdate(),'" + Flag + "','" + Content + "','" + Other + "')";
DAL Da = new DAL();
Da.ExecuteNonQuery(Sql);*/
}
///
/// 得到FTP服务器文件列表
///
///
public string[] GetFileList(string Key)
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpOutPath + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.UsePassive = false;
reqFTP.Proxy=null;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (Key == "" || line.IndexOf(Key) >= 0)
{
result.Append(line);
result.Append("\n");
}
line = reader.ReadLine();
}
if (result.Length < 1)
{
downloadFiles = null;
}
else
{
result.Remove(result.ToString().LastIndexOf('\n'), 1);
downloadFiles = result.ToString().Split('\n');
// 删除最后的 '\n'
}
reader.Close();
response.Close();
}
catch (Exception Err)
{
downloadFiles = null;
DataTransLog("失败", Key + "文件获取出错:" + Err.Message, "FtpWeb.GetFileList");
}
return downloadFiles;
}
///
/// 下载文到本地
///
///
///
public bool Download(string filePath, string fileName)
{
bool Result = false;
try
{
FtpWebRequest reqFTP;
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpOutPath + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
Result = true;
}
catch (Exception Err)
{
DataTransLog("失败", filePath + fileName + "文件下载出错:" + Err.Message, "FtpWeb.DownLoad," + fileName);
}
return Result;
}
///
/// 删除文件
///
///
public bool DeleteFile(string fileName)
{
bool Result = false;
try
{
string uri = "ftp://" + ftpServerIP + "/" + ftpOutPath + "/" + fileName;
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
Result = true;
}
catch (Exception Err)
{
DataTransLog("失败", fileName + "文件删除出错:" + Err.Message, "FtpWeb.DeleteFile," + fileName);
}
return Result;
}
/
/ 移动文件
/
/
/
//public bool MoveToFile(string fileName)
//{
// bool result = false;
// try
// {
// string uri = "ftp://" + ftpServerIP + "/" + ftpInPath + "/" + fileName;
// FtpWebRequest reqFTP;
// // 根据uri创建FtpWebRequest对象
// reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// // ftp用户名和密码
// reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// // 默认为true,连接不会被关闭
// // 在一个命令之后被执行
// reqFTP.KeepAlive = false;
// // 指定执行什么命令
// reqFTP.Method = WebRequestMethods.Ftp.Rename;
// FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
// response.Close();
// Result = true;
// }
//}
}
}