使用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文件相关推荐
- [转]文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)实现汇总1
转自:http://blog.csdn.net/soarheaven/archive/2008/12/08/3474152.aspx 最近项目需要对FTP服务器进行操作,现把实现总结如下: 打算分2篇 ...
- Microsoft .NET Framework 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)实现汇总1...
相关文章导航 Sql Server2005 Transact-SQL 新兵器学习总结之-总结 Flex,Fms3相关文章索引 FlexAir开源版-全球免费多人视频聊天室,免费网络远程多人视频会议系统 ...
- springboot文件上传下载实战 —— 登录功能、展示所有文件
springboot文件上传下载实战 创建项目 pom.xml 数据库建表与环境准备 建表SQL 配置文件 application.properties 整体架构 前端页面 登录页面 login.ht ...
- minio实现文件上传下载和删除功能
前言 之前用到文件上传功能,在这里做个学习记录.使用minio实现,后面会记录使用fastdfs和阿里云的oss实现文件上传以及他们的比较(oss根据流量收费).minio的中文文档:https:// ...
- java歌曲上传下载功能实现,Java上传下载功能的实现详解
上传下载是很简单的功能,但是每次用的时候还是要查,这里整理一下 前台: 提交 主要注意: enctype="multipart/form-data" method="po ...
- 百度云和阿里云实现文件上传下载和删除功能
百度云和阿里云类似,本文以百度云为例. 创建百度云对象存储实例 1. 进入百度云官网找到对象存储BOS 2.创建Bucket 3.客户端认证信息 accessKeyId和accessKeySecret ...
- ftp上传工具 免安装,ftp上传工具 免安装绿色破解版好用推荐
ftp上传工具是能够免安装的你知道吗,你有特别喜欢的ftp上传工具吗,如果你对这方面不懂的话,但是现在又急着用这款的情况下,你刷到了我这篇文章,那么恭喜你,你已经成功了一半了.今天网就来给大家介绍一款 ...
- ftp上传工具中文版,有好用的中文版ftp上传工具吗?ftp上传工具中文版
常用的ftp服务器有server-U.FileZilla.iis7服务器管理工具ftp客户端工具等,其中iis7服务器管理工具最为方便,不仅能批量管理ftp站点,还能定时上传和下载文件.此外,iis7 ...
- ftp上传工具,ftp上传工具哪个好?ftp上传工具使用指南,6款好用的ftp上传工具推荐
一提到ftp上传工具,大家第一个想到的不知道是什么,有可能你不太熟悉,但从事网站管理人员一定都用过.ftp上传工具是网络上用来传送文件的工具.ftp上传工具以汉化版和破解版的居多.但是破解版的一般都比 ...
- 云服务器 ftp上传文件大小,云服务器 ftp上传文件大小
云服务器 ftp上传文件大小 内容精选 换一换 当您有如下需求时,可以考虑使用文件注入功能将文件注入到弹性云服务器:需要通过脚本简化弹性云服务器配置通过脚本初始化系统已有脚本,在创建弹性云服务器的时候 ...
最新文章
- 转载iOS开发中常见的警告及错误
- 19个人工智能(AI)热门应用领域,你知道多少?
- docker 时间不一致,时区设置
- 【机器学习基础】浅析机器学习集成学习与模型融合
- 批量清除为Button注册的事件
- JAVA基础知识之网络编程——-基于AIO的异步Socket通信
- TQ210 —— s5pv210 Linux内核结构
- 怎么监听Android软键盘的打开和关闭
- 《哪吒之魔童降世》电影密钥延期至9月26日,将冲击中国票房总榜前三
- seafile看不见repo报500错误的解决方法
- 配置服务器获取信息失败,获取配置服务器失败
- 程序员不好好写报告的结果是很严重的
- typescript 装饰器
- AD16实现板框挖空
- 爬取上交所公司信息,根据公司股票代码获取公司注册地址以及地址的经纬度
- Python全局变量的隐藏“窍门”
- 三层架构(我的理解及详细分析)
- 随机失活 dropout直观理解
- Mac上Logi Options安装问题
- 自杀和抑郁症是为了进化?