using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net; namespace FTPConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//测试
//DownloadFtp("优课.png");
//UploadFtp("FTPConsoleApplication.exe.config");
//SystemLog.logger("OK");
} public static FtpStatusCode UploadFileInFTP(string filename)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
FtpWebRequest uploadRequest = null;
string serverIP;
string userName;
string password;
string uploadurl; try
{
serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];
userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
password = System.Configuration.ConfigurationManager.AppSettings["Password"];
uploadurl = "ftp://" + serverIP + "/" + Path.GetFileName(filename);
uploadRequest = (FtpWebRequest)WebRequest.Create(uploadurl);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Proxy = null;
NetworkCredential nc = new NetworkCredential();
nc.UserName = userName;
nc.Password = password;
uploadRequest.Credentials = nc;
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(filename, FileMode.Open); byte[] buffer = new byte[];
int bytesRead; while (true)
{
bytesRead = fileStream.Read(buffer, , buffer.Length);
if (bytesRead == )
{
break;
}
requestStream.Write(buffer, , bytesRead);
} requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
return uploadResponse.StatusCode;
}
catch (Exception e)
{
SystemLog.logger(e.InnerException.Message);
}
finally
{
if (uploadResponse != null)
{
uploadResponse.Close();
}
if (fileStream != null)
{
fileStream.Close();
}
if (requestStream != null)
{
requestStream.Close();
} }
return FtpStatusCode.Undefined;
} public static int UploadFtp(string filename)
{
FtpWebRequest reqFTP = null;
string serverIP;
string userName;
string password;
string url; try
{
FileInfo fileInf = new FileInfo(filename);
serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];
userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
password = System.Configuration.ConfigurationManager.AppSettings["Password"];
url = "ftp://" + serverIP + "/" + Path.GetFileName(filename); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFTP.Credentials = new NetworkCredential(userName, password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length; int buffLength = ;
byte[] buff = new byte[buffLength];
int contentLen; FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); Stream strm = reqFTP.GetRequestStream(); contentLen = fs.Read(buff, , buffLength); while (contentLen != )
{ strm.Write(buff, , contentLen);
contentLen = fs.Read(buff, , buffLength);
} strm.Close();
fs.Close();
return ;
}
catch (Exception ex)
{
if (reqFTP != null)
{
reqFTP.Abort();
}
SystemLog.logger(ex.InnerException.Message);
return -;
}
} public static int DownloadFtp(string filename)
{
FtpWebRequest reqFTP;
string serverIP;
string userName;
string password;
string url; try
{
serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];
userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
password = System.Configuration.ConfigurationManager.AppSettings["Password"];
url = "ftp://" + serverIP + "/" + Path.GetFileName(filename); FileStream outputStream = new FileStream(filename, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(userName, password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = ;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, , bufferSize);
while (readCount > )
{
outputStream.Write(buffer, , readCount);
readCount = ftpStream.Read(buffer, , bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return ;
}
catch (Exception ex)
{
SystemLog.logger(ex.InnerException.Message);
return -;
}
} public class SystemLog
{ public static bool logger(string message)
{
try
{
DateTime timeNow = DateTime.Now;
string logPath = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
string logSwitch = System.Configuration.ConfigurationManager.AppSettings["LogSwitch"];
if (logSwitch == "")
{
string logFullPath = Path.Combine(System.Environment.CurrentDirectory,logPath);
DirectoryInfo dirInfo = new DirectoryInfo(logFullPath);
if (!dirInfo.Exists)
{
dirInfo.Create();
} Encoding encoding = Encoding.GetEncoding("gb2312");
byte[] info = encoding.GetBytes("[ " + timeNow.ToString("yyyy-MM-dd HH:mm:ss") + " ] " + message + "\n"); //转换编码成字节串 if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
logPath = logPath + Path.DirectorySeparatorChar.ToString();
} using (FileStream fs = System.IO.File.Open(logPath + timeNow.ToString("yyyy_MM_dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
fs.Write(info, , info.Length);
//以ASCII方式编写
using (StreamWriter w = new StreamWriter(fs, Encoding.ASCII))
{
w.Flush();
w.Close();
} fs.Close();
}
}
return true;
}
catch (Exception e)
{
return false;
}
}
} } }

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. 【BZOJ1135】[POI2009]Lyz

    题解: hall定理..第一次听说 思考了半小时无果 二分图匹配时间显然太大 但是有这个hall定理 二分图有完美匹配的充要条件是 对于左边任意一个集合(大小为|s|),其连边点构成的集合(大小为|s ...

  2. 日常推荐大神操作,如何再oracle中delete数据后恢复

    http://blog.csdn.net/wangdinghai365/article/details/8593869

  3. HDU 1348 Wall 【凸包】

    <题目链接> 题目大意: 给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入. 解题分析: 求 ...

  4. 安装JDK提示: 该项不适于在指定状态下使用的错误

    解决办法有两个,两个办法不相关,运用其中一个就能解决问题. 解决方法:http://www.360doc.com/content/15/0407/14/19179788_461278604.shtml

  5. Python学习——迭代器&生成器&装饰器

    一.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退迭代器的一大优点是不要求事先准备好整个迭代过程中所有的元素.迭代器仅 ...

  6. SQL 中详解round(),floor(),ceiling()函数的用法和区别?

    SQL 中详解round(),floor(),ceiling()函数的用法和区别? 原创 2013年06月09日 14:00:21   摘自:http://blog.csdn.net/yueliang ...

  7. [AGC025B]RGB Coloring

    [AGC025B]RGB Coloring 题目大意: 有\(n(n\le3\times10^5)\)个格子,每个格子可以选择涂成红色.蓝色.绿色或不涂色,三种颜色分别产生\(a,b,a+b(a,b\ ...

  8. java程序的种类有三种

    Application―Java应用程序”是可以独立运行的Java程序.由Java解释器控制执行.Applet  ―Java小程序”不能独立运行(嵌入到Web页中).  由Java兼容浏览器控制执行. ...

  9. JavaScript_几种创建对象(2017-07-04)

    理解对象 1.创建Object实例 var clock = new Object(); clock.hour = 12; clock.minute = 10; clock.showHour = fun ...

  10. 喵哈哈村的魔法考试 Round #8 (Div.2) 题解

    喵哈哈村的美食面馆 签到题,就不停的if就好了. #include<bits/stdc++.h> using namespace std; string name[5]={"ni ...