使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件
使用如下類來實(shí)現(xiàn)ftp上操作xml遇到如下問題:
1、在公司內(nèi)網(wǎng)中的服務(wù)器上搭建ftp來操作,一切正常;但是,當(dāng)連接客戶的機(jī)器的時(shí)候,出現(xiàn)亂碼;
2、加上WebProxy后,客戶端是xml格式的,下載到本地卻是html格式的;
也不曉得問題到底出在哪里?。。。。
分析客戶的ftp和內(nèi)網(wǎng)中ftp的區(qū)別:
客戶方的ftp是采用非標(biāo)準(zhǔn)的ssl端口連接,所以需要開啟相應(yīng)的ssl端口;(已開啟了的)
還請(qǐng)閱讀本篇文章的高手多多指點(diǎn)下了~~!
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連接成功后的當(dāng)前目錄, 如果不指定即默認(rèn)為根目錄</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>
??????? /// 獲取當(dāng)前目錄下明細(xì)(包含文件和文件夾)
??????? /// </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>
??????? /// 獲取當(dāng)前目錄下所有的文件夾列表(僅文件夾)
??????? /// </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 風(fēng)格*/
??????????????????? m += str.Substring(dirPos + 5).Trim() + "\n";
??????????????? }
??????????????? else if (str.Trim().Substring(0, 1).ToUpper() == "D")
??????????????? {
??????????????????? /*判斷 Unix 風(fēng)格*/
??????????????????? string dir = str.Substring(54).Trim();
??????????????????? if (dir != "." && dir != "..")
??????????????????? {
??????????????????????? m += dir + "\n";
??????????????????? }
??????????????? }
??????????? }
??????????? char[] n = new char[] { '\n' };
??????????? return m.Split(n);
??????? }
??????? /// <summary>
??????? /// 判斷當(dāng)前目錄下指定的文件是否存在
??????? /// </summary>
??????? /// <param name="RemoteFileName">遠(yuǎn)程文件名</param>
??????? public bool FileExist(string RemoteFileName)
??????? {
??????????? string[] fileList = GetDirectoryList();
??????????? foreach (string str in fileList)
??????????? {
??????????????? if (str.Trim() == RemoteFileName.Trim())
??????????????? {
??????????????????? return true;
??????????????? }
??????????? }
??????????? return false;
??????? }
??????? /// <summary>
??????? /// 切換當(dāng)前目錄
??????? /// </summary>
??????? /// <param name="DirectoryName"></param>
??????? /// <param name="IsRoot">true 絕對(duì)路徑?? false 相對(duì)路徑</param>
??????? public string GotoDirectory(string DirectoryName, bool IsRoot)
??????? {
??????????? if (IsRoot)
??????????? {
??????????????? ftpRemotePath = DirectoryName;
??????????? }
??????????? else
??????????? {
??????????????? ftpRemotePath += DirectoryName + "/";
??????????? }
??????????? ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
??????????? return ftpURI;
??????? }
轉(zhuǎn)載于:https://www.cnblogs.com/xia_mi/archive/2011/05/30/2063425.html
總結(jié)
以上是生活随笔為你收集整理的使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 专用照明灯具(放眩晕)SPLTL3123
- 下一篇: 3030工业铝型材适合用哪种规格的螺栓?