1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net;
  7.  
  8. namespace FTPConsoleApplication
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //测试
  15. //DownloadFtp("优课.png");
  16. //UploadFtp("FTPConsoleApplication.exe.config");
  17. //SystemLog.logger("OK");
  18. }
  19.  
  20. public static FtpStatusCode UploadFileInFTP(string filename)
  21. {
  22. Stream requestStream = null;
  23. FileStream fileStream = null;
  24. FtpWebResponse uploadResponse = null;
  25. FtpWebRequest uploadRequest = null;
  26. string serverIP;
  27. string userName;
  28. string password;
  29. string uploadurl;
  30.  
  31. try
  32. {
  33. serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];
  34. userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
  35. password = System.Configuration.ConfigurationManager.AppSettings["Password"];
  36. uploadurl = "ftp://" + serverIP + "/" + Path.GetFileName(filename);
  37. uploadRequest = (FtpWebRequest)WebRequest.Create(uploadurl);
  38. uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
  39. uploadRequest.Proxy = null;
  40. NetworkCredential nc = new NetworkCredential();
  41. nc.UserName = userName;
  42. nc.Password = password;
  43. uploadRequest.Credentials = nc;
  44. requestStream = uploadRequest.GetRequestStream();
  45. fileStream = File.Open(filename, FileMode.Open);
  46.  
  47. byte[] buffer = new byte[];
  48. int bytesRead;
  49.  
  50. while (true)
  51. {
  52. bytesRead = fileStream.Read(buffer, , buffer.Length);
  53. if (bytesRead == )
  54. {
  55. break;
  56. }
  57. requestStream.Write(buffer, , bytesRead);
  58. }
  59.  
  60. requestStream.Close();
  61. uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
  62. return uploadResponse.StatusCode;
  63. }
  64. catch (Exception e)
  65. {
  66. SystemLog.logger(e.InnerException.Message);
  67. }
  68. finally
  69. {
  70. if (uploadResponse != null)
  71. {
  72. uploadResponse.Close();
  73. }
  74. if (fileStream != null)
  75. {
  76. fileStream.Close();
  77. }
  78. if (requestStream != null)
  79. {
  80. requestStream.Close();
  81. }
  82.  
  83. }
  84. return FtpStatusCode.Undefined;
  85. }
  86.  
  87. public static int UploadFtp(string filename)
  88. {
  89. FtpWebRequest reqFTP = null;
  90. string serverIP;
  91. string userName;
  92. string password;
  93. string url;
  94.  
  95. try
  96. {
  97. FileInfo fileInf = new FileInfo(filename);
  98. serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];
  99. userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
  100. password = System.Configuration.ConfigurationManager.AppSettings["Password"];
  101. url = "ftp://" + serverIP + "/" + Path.GetFileName(filename);
  102.  
  103. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  104. reqFTP.Credentials = new NetworkCredential(userName, password);
  105. reqFTP.KeepAlive = false;
  106. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  107. reqFTP.UseBinary = true;
  108. reqFTP.ContentLength = fileInf.Length;
  109.  
  110. int buffLength = ;
  111. byte[] buff = new byte[buffLength];
  112. int contentLen;
  113.  
  114. FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  115.  
  116. Stream strm = reqFTP.GetRequestStream();
  117.  
  118. contentLen = fs.Read(buff, , buffLength);
  119.  
  120. while (contentLen != )
  121. {
  122.  
  123. strm.Write(buff, , contentLen);
  124. contentLen = fs.Read(buff, , buffLength);
  125. }
  126.  
  127. strm.Close();
  128. fs.Close();
  129. return ;
  130. }
  131. catch (Exception ex)
  132. {
  133. if (reqFTP != null)
  134. {
  135. reqFTP.Abort();
  136. }
  137. SystemLog.logger(ex.InnerException.Message);
  138. return -;
  139. }
  140. }
  141.  
  142. public static int DownloadFtp(string filename)
  143. {
  144. FtpWebRequest reqFTP;
  145. string serverIP;
  146. string userName;
  147. string password;
  148. string url;
  149.  
  150. try
  151. {
  152. serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];
  153. userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
  154. password = System.Configuration.ConfigurationManager.AppSettings["Password"];
  155. url = "ftp://" + serverIP + "/" + Path.GetFileName(filename);
  156.  
  157. FileStream outputStream = new FileStream(filename, FileMode.Create);
  158. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  159. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  160. reqFTP.UseBinary = true;
  161. reqFTP.KeepAlive = false;
  162. reqFTP.Credentials = new NetworkCredential(userName, password);
  163. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  164. Stream ftpStream = response.GetResponseStream();
  165. long cl = response.ContentLength;
  166. int bufferSize = ;
  167. int readCount;
  168. byte[] buffer = new byte[bufferSize];
  169. readCount = ftpStream.Read(buffer, , bufferSize);
  170. while (readCount > )
  171. {
  172. outputStream.Write(buffer, , readCount);
  173. readCount = ftpStream.Read(buffer, , bufferSize);
  174. }
  175. ftpStream.Close();
  176. outputStream.Close();
  177. response.Close();
  178. return ;
  179. }
  180. catch (Exception ex)
  181. {
  182. SystemLog.logger(ex.InnerException.Message);
  183. return -;
  184. }
  185. }
  186.  
  187. public class SystemLog
  188. {
  189.  
  190. public static bool logger(string message)
  191. {
  192. try
  193. {
  194. DateTime timeNow = DateTime.Now;
  195. string logPath = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
  196. string logSwitch = System.Configuration.ConfigurationManager.AppSettings["LogSwitch"];
  197. if (logSwitch == "")
  198. {
  199. string logFullPath = Path.Combine(System.Environment.CurrentDirectory,logPath);
  200. DirectoryInfo dirInfo = new DirectoryInfo(logFullPath);
  201. if (!dirInfo.Exists)
  202. {
  203. dirInfo.Create();
  204. }
  205.  
  206. Encoding encoding = Encoding.GetEncoding("gb2312");
  207. byte[] info = encoding.GetBytes("[ " + timeNow.ToString("yyyy-MM-dd HH:mm:ss") + " ] " + message + "\n"); //转换编码成字节串
  208.  
  209. if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
  210. {
  211. logPath = logPath + Path.DirectorySeparatorChar.ToString();
  212. }
  213.  
  214. using (FileStream fs = System.IO.File.Open(logPath + timeNow.ToString("yyyy_MM_dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
  215. {
  216. fs.Write(info, , info.Length);
  217. //以ASCII方式编写
  218. using (StreamWriter w = new StreamWriter(fs, Encoding.ASCII))
  219. {
  220. w.Flush();
  221. w.Close();
  222. }
  223.  
  224. fs.Close();
  225. }
  226. }
  227. return true;
  228. }
  229. catch (Exception e)
  230. {
  231. return false;
  232. }
  233. }
  234. }
  235.  
  236. }
  237.  
  238. }

FTP上传、下载(简单小例子)的更多相关文章

  1. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  2. windows系统下ftp上传下载和一些常用命令

    先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...

  3. windows下ftp上传下载和一些常用命令

    先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...

  4. FTP上传下载工具(FlashFXP) v5.5.0 中文版

    软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...

  5. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

  6. C# -- FTP上传下载

    C# -- FTP上传下载 1. C#实现FTP下载 private static void TestFtpDownloadFile(string strFtpPath, string strFile ...

  7. Java.ftp上传下载

    1:jar的maven的引用: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  8. python之实现ftp上传下载代码(含错误处理)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之实现ftp上传下载代码(含错误处理) #http://www.cnblogs.com/kait ...

  9. python之模块ftplib(实现ftp上传下载代码)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ftplib(实现ftp上传下载代码) #需求:实现ftp上传下载代码(不含错误处理) f ...

  10. java客户端调用ftp上传下载文件

    1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...

随机推荐

  1. 【Java】 剑指offer(10) 旋转数组的最小数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. ...

  2. 013 Spark中的资源调优

    1.平常的资源使用情况 2.官网 3.资源参数调优 cores memory JVM 4.具体参数 可以在--conf参数中给定资源配置相关信息(配置的一般是JVM的一些垃圾回收机制) --drive ...

  3. [OpenCV-Python] OpenCV 中的 Gui特性 部分 II

    部分 IIOpenCV 中的 Gui 特性 OpenCV-Python 中文教程(搬运)目录 4 图片 目标 • 在这里你将学会怎样读入一幅图像,怎样显示一幅图像,以及如何保存一幅图像 • 你将要学习 ...

  4. 《Gradle权威指南》--Gradle任务

    No1: 多种方式创建任务 def Task ex41CreateTask1 = task(ex41CreateTask1) ex41CreateTask1.doLast{ println " ...

  5. VUE 打包后关于 -webkit-box-orient: vertical; 消失,导致多行溢出不管用问题

    VUE 打包后 -webkit-box-orient: vertical; 样式消失,导致页面样式爆炸,看了看解决方案,在这里总结一下: 实际上是 optimize-css-assets-webpac ...

  6. BufferedReader的小例子

    注意: BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用: BufferedReader buf = ...

  7. VB打开工程时出现不能加载MSCOMCTL.OCX

    用记事本打开VBP文件找到这一行:Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.1#0; MSCOMCTL.OCX改为:Object={831FDD1 ...

  8. NMAP为什么扫描不到端口

    NMAP为什么扫描不到端口   NMAP是知名的网络端口扫描工具.但很多新人发现,使用NMAP经常扫描不出来任何端口,尤其是手机之类.这实际存在一个理解上的误区.扫描端口是为了发现主机/设备上存在的对 ...

  9. 8.9 正睿暑期集训营 Day6 C 风花雪月(DP)

    题目链接 完整比赛在这儿. 杜老师tql . 求期望要抽卡的次数,也就是求期望经历了多少不满足状态.而每个不满足的状态对答案的贡献为\(1\),所以可以直接算概率.即\(Ans=\sum_{不满足状态 ...

  10. Saltstack cp.get 模块

    语法 salt '*' cp.get_file salt://rr /etc/rr cp.get_url  可以从一个URL地址下载文件,URL可以是msater上的路径(salt://),也可以是h ...