//FileStream, FileInfo等类只能处理本地服务器文件,无法读取客户端文件;读取文件流要使用Stream
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;
}
}
/// <summary>
/// 功能:删除Ftp文件 (在更新数据的同时将Ftp上的文件删除)
///撰写时间:20100427
/// </summary>
/// <param name="strFtpFileName"></param>
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();
}
/// <summary>
///功能:下载ftp文档
///撰写时间:20100427
/// </summary>
/// <param name="strFtpFileName"></param>
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();
}
/// <summary>
/// 功能:检查文件是否存在
///撰写时间:20100427
/// </summary>
/// <param name="strFtpURL"></param>
/// <param name="ftpUserName"></param>
/// <param name="ftpPwd"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 功能:重命名文件
///撰写时间:20100427
/// </summary>
/// <param name="currentFilename"></param>
/// <param name="newFilename"></param>
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;
}
/// <summary>
/// 文件存在检查
/// </summary>
/// <param name="ftpName"></param>
/// <returns></returns>
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;
}