当前位置:
首页 > Python基础教程 >
-
C#教程之Ftp操作类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Solution.Common { public class FtpHelper { private FtpHelper() { } private static FtpHelper _instance; private static readonly object obj = new object(); //懒汉式,双重检查锁定 public static FtpHelper GetInstance() { if (_instance == null) { lock (obj) { if (_instance == null) { _instance = new FtpHelper(); } } } return _instance; } /// <summary> /// 将生成的内存流上传到Ftp服务器,并生成文件 /// </summary> /// <param name="ms">待上传的文件内存流</param> /// <param name="userid">ftp账号</param> /// <param name="pwd">ftp密码</param> /// <param name="filename">待生成文件的名称</param> /// <param name="ftpPath">文件在ftp存放的路径</param> /// <param name="msg">返回的消息</param> /// <returns></returns> public bool Upload(MemoryStream ms, string userid, string pwd, string filename, string ftpPath, out string msg) { bool isSuccess = false; try { StreamWriter data_writer = new StreamWriter(ms);//为指定的文件流生成StreamWriter实例 FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + filename)); ftp.Credentials = new NetworkCredential(userid, pwd);//为指定的文件名称和密码 ftp.KeepAlive = false; //指定执行命令 ftp.Method = WebRequestMethods.Ftp.UploadFile; //指定数据传输类型 ftp.UseBinary = true; //15秒超时 ftp.Timeout = 15000; using (Stream ftp_stream = ftp.GetRequestStream()) { //指向内存流头 ms.Position = 0; ms.CopyTo(ftp_stream); data_writer.Close(); ms.Close(); ftp_stream.Close(); } msg = "上传成功"; isSuccess = true; } catch (Exception ex) { msg = ex.Message; isSuccess = false; } return isSuccess; } /// <summary> /// 删除ftp服务器中的文件 /// </summary> /// <param name="userId">ftp账号</param> /// <param name="pwd">ftp密码</param> /// <param name="ftpPath">ftp中文件的路径</param> /// <param name="fileName">文件名称</param> public bool Delete(string userId, string pwd, string ftpPath, string fileName) { FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName)); reqFTP.Method = WebRequestMethods.Ftp.DeleteFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(userId, pwd); reqFTP.UsePassive = false; FtpWebResponse listResponse = (FtpWebResponse)reqFTP.GetResponse(); listResponse.Close(); return true; //string sStatus = listResponse.StatusDescription; } catch (Exception ex) { return false; } } ///下载文件 //userId:ftp账号,pwd:ftp密码,filename:文件名(类似E:\DBCenter\text.xml),ftpPath:ftp路径,Msg:输出信息 public bool Download(string userId, string pwd, string ftpPath, string filePath, string fileName, out string Msg) { bool isSuccess = false; Msg = string.Empty; FtpWebRequest reqFTP; FtpWebRequest reqFTP1; try { FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName)); reqFTP.Method = WebRequestMethods.Ftp.GetFileSize; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(userId, pwd); reqFTP.UsePassive = true; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); long cl = response.ContentLength; response.Close(); if (cl > 0) { reqFTP1 = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName)); reqFTP1.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP1.UseBinary = true; reqFTP1.Credentials = new NetworkCredential(userId, pwd); reqFTP1.UsePassive = true; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; FtpWebResponse response1 = (FtpWebResponse)reqFTP1.GetResponse(); Stream ftpStream = response1.GetResponseStream(); readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); isSuccess = true; } ftpStream.Close(); outputStream.Close(); response1.Close(); } } catch(Exception ex) { isSuccess = false; Msg = "程序出现异常:" + ex.Message; } return isSuccess; } } }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式