日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件

發布時間:2024/9/20 asp.net 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用如下類來實現ftp上操作xml遇到如下問題:

1、在公司內網中的服務器上搭建ftp來操作,一切正常;但是,當連接客戶的機器的時候,出現亂碼;

2、加上WebProxy后,客戶端是xml格式的,下載到本地卻是html格式的;
也不曉得問題到底出在哪里?。。。。

分析客戶的ftp和內網中ftp的區別:

客戶方的ftp是采用非標準的ssl端口連接,所以需要開啟相應的ssl端口;(已開啟了的)

還請閱讀本篇文章的高手多多指點下了~~!

public class FtpWeb_Delete
??? {
??????? private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(FtpWeb_Delete));
??????? string ftpServerIP;
??????? string ftpRemotePath;
??????? string ftpOutPutPath;
??????? string ftpUserID;
??????? string ftpPassword;
??????? string ftpURI;
??????? //string proxyName;
??????? //string proxyPass;
??????? private WebProxy _proxy = null;
??????? public WebProxy proxy
??????? {
??????????? get
??????????? {
??????????????? return _proxy;
??????????? }
??????????? set
??????????? {
??????????????? _proxy = value;
??????????? }
??????? }

??????? /// <summary>
??????? /// 連接FTP
??????? /// </summary>
??????? /// <param name="FtpServerIP">FTP連接地址</param>
??????? /// <param name="FtpRemotePath">指定FTP連接成功后的當前目錄, 如果不指定即默認為根目錄</param>
??????? /// <param name="FtpUserID">用戶名</param>
??????? /// <param name="FtpPassword">密碼</param>
??????? public FtpWeb_Delete(string FtpServerIP, string FtpRemotePath, string FtpOutPutPath, string FtpUserID, string FtpPassword)
??????? {
??????????? ftpServerIP = FtpServerIP;
??????????? ftpRemotePath = FtpRemotePath;
??????????? ftpOutPutPath = FtpOutPutPath;
??????????? ftpUserID = FtpUserID;
??????????? ftpPassword = FtpPassword;
??????????? ftpURI = "ftp://" + ftpServerIP + "/";
??????????? //this._proxy = objProxy;
??????????? //objProxy.Credentials = new NetworkCredential(this.proxyName,this.proxyPass);
??????? }

??????? /// <summary>
??????? /// 上傳
??????? /// </summary>
??????? /// <param name="filename"></param>
??????? public void Upload(string filename)
??????? {
??????????? FileInfo fileInf = new FileInfo(filename);
??????????? string uri = GotoDirectory(ftpOutPutPath, true) + fileInf.Name;
??????????? //string uri = ftpURI + fileInf.Name;
??????????? FtpWebRequest reqFTP;

??????????? reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
??????????? reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
??????????? reqFTP.KeepAlive = false;
??????????? reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
??????????? reqFTP.UseBinary = true;
??????????? //reqFTP.Credentials = new NetworkCredential(this.proxyName,this.proxyPass);
??????????? if (this._proxy != null)
??????????? {
??????????????? reqFTP.Proxy = this._proxy;
??????????? }
??????????? reqFTP.ContentLength = fileInf.Length;
??????????? int buffLength = 2048;
??????????? byte[] buff = new byte[buffLength];
??????????? int contentLen;
??????????? FileStream fs = fileInf.OpenRead();
??????????? try
??????????? {
??????????????? Stream strm = reqFTP.GetRequestStream();
??????????????? contentLen = fs.Read(buff, 0, buffLength);
??????????????? while (contentLen != 0)
??????????????? {
??????????????????? strm.Write(buff, 0, contentLen);
??????????????????? contentLen = fs.Read(buff, 0, buffLength);
??????????????? }
??????????????? strm.Close();
??????????????? fs.Close();
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? log.Error("FtpWeb,Upload Error -->", ex);
??????????? }
??????? }

??????? /// <summary>
??????? /// 下載
??????? /// </summary>
??????? /// <param name="filePath"></param>
??????? /// <param name="fileName"></param>
??????? public void Download(string filePath, string fileName)
??????? {
??????????? FtpWebRequest reqFTP;
??????????? try
??????????? {
??????????????? string uri = GotoDirectory(ftpRemotePath, true) + fileName;
??????????????? FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
??????????????? reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
??????????????? reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
??????????????? reqFTP.UseBinary = true;
??????????????? reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
??????????????? if (this._proxy != null)
??????????????? {
??????????????????? reqFTP.Proxy = this._proxy;
??????????????? }
??????????????? 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();
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? log.Error("FtpWeb,Download Error --> ", ex);
??????????? }
??????? }

??????? /// <summary>
??????? /// 刪除文件
??????? /// </summary>
??????? /// <param name="fileName"></param>
??????? public void Delete(string fileName)
??????? {
??????????? try
??????????? {
??????????????? string uri = GotoDirectory(ftpRemotePath, true) + "/" + fileName;
??????????????? //string uri = ftpURI + fileName;
??????????????? FtpWebRequest reqFTP;
??????????????? reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

??????????????? reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
??????????????? //reqFTP.EnableSsl = true;//用戶名加密
??????????????? reqFTP.KeepAlive = false;
??????????????? reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;

??????????????? string result = String.Empty;
??????????????? FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
??????????????? long size = response.ContentLength;
??????????????? Stream datastream = response.GetResponseStream();
??????????????? StreamReader sr = new StreamReader(datastream);
??????????????? result = sr.ReadToEnd();
??????????????? sr.Close();
??????????????? datastream.Close();
??????????????? response.Close();
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? log.Error("FtpWeb,Delete Error --> ", ex);

??????????? }
??????? }

??????? /// <summary>
??????? /// 獲取當前目錄下明細(包含文件和文件夾)
??????? /// </summary>
??????? /// <returns></returns>
??????? public string[] GetFilesDetailList()
??????? {
??????????? string[] downloadFiles;
??????????? try
??????????? {
??????????????? StringBuilder result = new StringBuilder();
??????????????? FtpWebRequest ftp;
??????????????? //ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
??????????????? ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
??????????????? ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
??????????????? ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
??????????????? WebResponse response = ftp.GetResponse();
??????????????? StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
??????????????? string line = reader.ReadLine();
??????????????? while (line != null)
??????????????? {
??????????????????? result.Append(line);
??????????????????? result.Append("\n");
??????????????????? line = reader.ReadLine();
??????????????? }
??????????????? result.Remove(result.ToString().LastIndexOf("\n"), 1);
??????????????? reader.Close();
??????????????? response.Close();
??????????????? return result.ToString().Split('\n');
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? downloadFiles = null;
??????????????? log.Error("FtpWeb,GetFilesDetailList Error --> ", ex);
??????????????? return downloadFiles;
??????????? }
??????? }
??????? /// <summary>
??????? /// 獲取文件
??????? /// </summary>
??????? /// <returns></returns>

??????? public string[] GetFileList()
??????? {
??????????? string[] downloadFiles;
??????????? StringBuilder result = new StringBuilder();
??????????? FtpWebRequest reqFTP;
??????????? string uri = GotoDirectory(ftpRemotePath, true);
??????????? try
??????????? {
??????????????? reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
??????????????? reqFTP.UseBinary = true;
??????????????? reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
??????????????? reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
??????????????? WebResponse response = reqFTP.GetResponse();
??????????????? StreamReader reader = new StreamReader(response.GetResponseStream());
??????????????? string line = reader.ReadLine();
??????????????? while (line != null)
??????????????? {
??????????????????? result.Append(line);
??????????????????? result.Append("\n");
??????????????????? line = reader.ReadLine();
??????????????? }
??????????????? // to remove the trailing 'n'
??????????????? if (result.Length != 0)
??????????????? {
??????????????????? result.Remove(result.ToString().LastIndexOf('\n'), 1);//提醒
??????????????? }
??????????????? reader.Close();
??????????????? response.Close();
??????????????? if (result.Length != 0)
??????????????? {
??????????????????? return result.ToString().Split('\n');
??????????????? }
??????????????? else
??????????????? {
??????????????????? return null;
??????????????? }

??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? log.Error("FtpWeb,GetFileList Error --> ", ex);
??????????????? downloadFiles = null;
??????????????? return downloadFiles;
??????????? }
??????? }

??????? /// <summary>
??????? /// 獲取當前目錄下所有的文件夾列表(僅文件夾)
??????? /// </summary>
??????? /// <returns></returns>
??????? public string[] GetDirectoryList()
??????? {
??????????? string[] drectory = GetFilesDetailList();
??????????? string m = string.Empty;
??????????? foreach (string str in drectory)
??????????? {
??????????????? int dirPos = str.IndexOf("<DIR>");
??????????????? if (dirPos > 0)
??????????????? {
??????????????????? /*判斷 Windows 風格*/
??????????????????? m += str.Substring(dirPos + 5).Trim() + "\n";
??????????????? }
??????????????? else if (str.Trim().Substring(0, 1).ToUpper() == "D")
??????????????? {
??????????????????? /*判斷 Unix 風格*/
??????????????????? string dir = str.Substring(54).Trim();
??????????????????? if (dir != "." && dir != "..")
??????????????????? {
??????????????????????? m += dir + "\n";
??????????????????? }
??????????????? }
??????????? }

??????????? char[] n = new char[] { '\n' };
??????????? return m.Split(n);
??????? }

??????? /// <summary>
??????? /// 判斷當前目錄下指定的文件是否存在
??????? /// </summary>
??????? /// <param name="RemoteFileName">遠程文件名</param>
??????? public bool FileExist(string RemoteFileName)
??????? {
??????????? string[] fileList = GetDirectoryList();
??????????? foreach (string str in fileList)
??????????? {
??????????????? if (str.Trim() == RemoteFileName.Trim())
??????????????? {
??????????????????? return true;
??????????????? }
??????????? }
??????????? return false;
??????? }

??????? /// <summary>
??????? /// 切換當前目錄
??????? /// </summary>
??????? /// <param name="DirectoryName"></param>
??????? /// <param name="IsRoot">true 絕對路徑?? false 相對路徑</param>
??????? public string GotoDirectory(string DirectoryName, bool IsRoot)
??????? {
??????????? if (IsRoot)
??????????? {
??????????????? ftpRemotePath = DirectoryName;
??????????? }
??????????? else
??????????? {
??????????????? ftpRemotePath += DirectoryName + "/";
??????????? }
??????????? ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
??????????? return ftpURI;
??????? }

轉載于:https://www.cnblogs.com/xia_mi/archive/2011/05/30/2063425.html

總結

以上是生活随笔為你收集整理的使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。