//上傳檔案到FTP
public void UpLoadFileToFTP()
{
FtpWebRequest reqFTP;
// 根據uri建立FtpWebRequest對象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + strFtpFileName));
// ftp使用者名和密碼
reqFTP.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);
// 預設為true,連接配接不會被關閉
// 在一個指令之後被執行
reqFTP.KeepAlive = false;
// 指定執行什麼指令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定資料傳輸類型
reqFTP.UseBinary = true;
//reqFTP.Timeout = 36000;
// 上傳檔案時通知伺服器檔案的大小
reqFTP.ContentLength = fuClientFileUpload.PostedFile.ContentLength;
// 緩沖大小設定為10kb
int buffLength = 10240;//2048
byte[] buff = new byte[buffLength];
int contentLen;
// 打開一個檔案流 (System.IO.FileStream) 去讀上傳的檔案
//FileStream fs = fileInf.OpenRead();
Stream fs = fuClientFileUpload.PostedFile.InputStream;
try
{
// 把上傳的檔案寫入流
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();
}
catch (Exception ex)
{
throw ex;
}
}
//删除Ftp檔案 (在更新資料的同時将Ftp上的檔案删除)
public void DeleteFtpFile(string strFileName)
{
Uri uri = new Uri(strFtpUrl + strFileName);
FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(uri);
listRequest.Method = WebRequestMethods.Ftp.DeleteFile;
listRequest.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);
FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse();
}
//下載下傳ftp文檔
public void DownLoadFtpFile(string strFtpFileName)
{
string strFtpURL = ParameterUtil.GetSysParameterValue("ONLINENEWSFTPURL"); //在線新聞上傳FTP位址
string strFtpUserId = ParameterUtil.GetSysParameterValue("ONLINENEWSFTPACCOUNT"); //FTP使用者id
string strFtpUserPwd = ParameterUtil.GetSysParameterValue("ONLINENEWSFTPPWD"); //FTP使用者密碼
Uri uri = new Uri("ftp://10.127.0.1 ");
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(strFtpUserId, strFtpUserPwd);
byte[] newFileData = request.DownloadData(uri.ToString());
FileStream fs = new FileStream(@"d:/abc.txt", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(newFileData, 0, newFileData.Length);
fs.Close();
}
//檢查檔案是否存在
public bool IsFileExist(string strFileName)
{
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + strFileName));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;// WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
if (line.Length > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
//重命名檔案
private bool fileRename(string currentFileName, string newFileName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
FtpWebResponse ftpWebResponse = null;
Stream ftpResponseStream = null;
try
{
string uri = strFtpUrl + currentFileName;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpWebRequest.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);
ftpWebRequest.UseBinary = true;
ftpWebRequest.Method = WebRequestMethods.Ftp.Rename;
ftpWebRequest.RenameTo = newFileName;
ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
ftpResponseStream = ftpWebResponse.GetResponseStream();
success = true;
}
catch (Exception)
{
success = false;
}
finally
{
if (ftpResponseStream != null)
{
ftpResponseStream.Close();
}
if (ftpWebResponse != null)
{
ftpWebResponse.Close();
}
}
return success;
}
// 檔案存在檢查
public bool fileCheckExist(string ftpName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
WebResponse webResponse = null;
StreamReader reader = null;
try
{
string url = strFtpUrl;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
ftpWebRequest.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpWebRequest.KeepAlive = false;
webResponse = ftpWebRequest.GetResponse();
reader = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.Default);
string line = reader.ReadLine();
while (line != null)
{
if (line == ftpName)
{
success = true;
break;
}
line = reader.ReadLine();
}
}
catch (Exception)
{
success = false;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (webResponse != null)
{
webResponse.Close();
}
}
return success;
}
本文轉自:http://blog.csdn.net/zhang_kevin_wei/article/details/5575650