FTP FtpWebRequest 异步上传文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Net;
namespace reporting.temp
{
public class FTP
{
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fullName;
private Exception operationException;
string statusCode;
string statusDescription;
string userName;
string password;
string ftpServerIP;
string fileName;
public Exception OperationException
{
get
{
return operationException;
}
set
{
operationException = value;
}
}
public ManualResetEvent OperationComplete
{
get { return wait; }
}
public FtpWebRequest Request
{
get
{
return request;
}
set
{
request = value;
}
}
public string FullName
{
get
{
return fullName;
}
set
{
fullName = value;
}
}
public string StatusCode
{
get
{
return statusCode;
}
set
{
statusCode = value;
}
}
public string StatusDescription
{
get
{
return statusDescription;
}
set
{
statusDescription = value;
}
}
public string UserName
{
get
{
return userName;
}
set
{
userName = value;
}
}
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
public string FtpServerIP
{
get
{
return ftpServerIP;
}
set
{
ftpServerIP = value;
}
}
public string FileName
{
get
{
return fileName;
}
set
{
fileName = value;
}
}
public FtpState(string ftpServerIP, string userName, string password)
{
FtpServerIP = ftpServerIP;
UserName = userName;
Password = password;
FileName = Path.GetFileName(fullName);
wait = new ManualResetEvent(false);
}
public void AsynchronousFtpUpLoader(string FullName)
{
FtpState AsyncState = new FtpState(FtpServerIP, UserName, Password);
AsyncState.FullName = FullName;
ManualResetEvent waitObject;
Uri target = new Uri("ftp://" + FtpServerIP + "/" + Path.GetFileName(FullName));
//FtpState AsyncState = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
//这个例子使用匿名登录。
//默认情况下,请求是匿名的; 证书不需要指定。
//这个示例仅指定凭据
//控制操作如何在服务器上登录。
request.Credentials = new NetworkCredential(AsyncState.UserName, AsyncState.Password);
//是否指定SSL 连接
request.EnableSsl = false;
//将请求存储在我们传入的对象中
//异步操作。
AsyncState.Request = request;
AsyncState.FullName = AsyncState.FullName;
//让事件继续等待。
waitObject = AsyncState.OperationComplete;
//异步获取文件内容的流。
request.BeginGetRequestStream(
new AsyncCallback(EndGetStreamCallback),
AsyncState
);
//阻塞当前线程,直到所有操作完成为止。
waitObject.WaitOne();
//操作要么完成,要么抛出异常。
if (AsyncState.OperationException != null)
throw AsyncState.OperationException;
else
Console.Write("Done-{0}", AsyncState.StatusDescription);
}
}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState AsyncState = (FtpState)ar.AsyncState;
Stream requestStream;
try
{
requestStream = AsyncState.Request.EndGetRequestStream(ar);
const int bufferLength = 2048;
byte[] _array = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream fs = File.OpenRead(AsyncState.FullName);
do
{
readBytes = fs.Read(_array, 0, bufferLength);
requestStream.Write(_array, 0, readBytes);
count += count;
} while (readBytes != 0);
//将字节写入流
Console.WriteLine("{0}-字节写入流");
fs.Close();
requestStream.Close();
AsyncState.Request.BeginGetResponse(
new AsyncCallback(EndGetResponseCallback),
AsyncState
);
}
catch (Exception e)
{
Console.WriteLine("错误响应:{0}", e.Message);
AsyncState.OperationComplete.Set();
AsyncState.OperationException = e;
throw;
}
}
private static void EndGetResponseCallback(IAsyncResult ar)
{
FtpState AsyncState = (FtpState)ar.AsyncState;
FtpWebResponse response;
try
{
response = (FtpWebResponse)AsyncState.Request.EndGetResponse(ar);
response.Close();
AsyncState.StatusCode = response.StatusCode.ToString();
AsyncState.StatusDescription = response.StatusDescription;
AsyncState.OperationComplete.Set();
}
catch (Exception e)
{
Console.WriteLine("错误响应:{0}", e.Message);
AsyncState.OperationComplete.Set();
AsyncState.OperationException = e;
}
}
}
}
FTP FtpWebRequest 异步上传文件的更多相关文章
- Servlet异步上传文件
这里需要用到插件ajaxfileupload.js,jar包:commons-fileupload-1.3.2.jar,commons-io-2.5.jar 注意红色部分的字!!!! 1.创建一个we ...
- struts2 jquery ajaxFileUpload 异步上传文件
网上搜集的,整理一下. 一.ajaxFileUpload 实现异步上传文件利用到了ajaxFileUpload.js这个文件,这是别人开发的一个jquery的插件,可以实现文件的上传并能够和strut ...
- 关于js异步上传文件
好久没登录博客园了,今天来一发分享. 最近项目里有个需求,上传文件(好吧,这种需求很常见,这也不是第一次遇到了).当时第一想法就是直接用form表单提交(原谅我以前就是这么干的),不过表单里不仅有文件 ...
- 利用ajaxfileupload.js异步上传文件
1.引入ajaxfileupload.js 2.html代码 <input type="file" id="enclosure" name="e ...
- 【转】JQuery插件ajaxFileUpload 异步上传文件(PHP版)
前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错,但是由于手机不支持flash,所以不得不再找一个文件上传插件来用了.后来发现a ...
- 异步上传文件,ajax上传文件,jQuery插件之ajaxFileUpload
http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 一.ajaxFileUpload是一个异步上传文件的jQuery插件. ...
- 利用jquery.form实现异步上传文件
实现原理 目前需要在一个页面实现多个地方调用上传控件上传文件,并且必须是异步上传.思考半天,想到通过创建动态表单包裹上传文件域,利用jquery.form实现异步提交表单,从而达到异步上传的目的,在上 ...
- HTML5预览图片、异步上传文件
注意啦:本文的代码都是以JQuery为示例,jq_开头的变量都是jq对象. 在HTML5中,我们可以在图片上传之前对图片进行预览,就像下面这么做 jq_upload_file.change(funct ...
- (H5)FormData+AJAX+SpringMVC跨域异步上传文件
最近都没时间整理资料了,一入职就要弄懂业务,整天被业务弄得血崩. 总结下今天弄了一个早上的跨域异步上传文件.主要用到技术有HTML5的FormData,AJAX,Spring MVC. 首先看下上传页 ...
随机推荐
- 洛谷—— P1450 [HAOI2008]硬币购物
P1450 [HAOI2008]硬币购物 硬币购物一共有$4$种硬币.面值分别为$c1,c2,c3,c4$.某人去商店买东西,去了$tot$次.每次带$di$枚$ci$硬币,买$si$的价值的东西.请 ...
- Number Theory Problem(The 2016 ACM-ICPC Asia China-Final Contest 找规律)
题目: Mr. Panda is one of the top specialists on number theory all over the world. Now Mr. Panda is in ...
- 网络基础——TCP/IP五层模型
TCP/IP五层模型 TCP/IP五层协议和OSI的七层协议对应关系如下 在每一层都工作着不同的设备,比如我们常用的交换机就工作在数据链路层的,一般的路由器是工作在网络层的. 在每一层实现的协议也各不 ...
- SQL学习笔记:一些高级语句
现在以MySQL为模板.学习的方法和别的数据库写法上会有不同,但是思路基本一致. 用到的数据库表的格式: +----+--------------+-------------------------- ...
- Java Syntax Specification
Java Syntax Specification Programs <compilation unit> ::= <package declaration>? <imp ...
- 初次使用Let's encrypt
wget --no-check-certificate -O shadowsocks.sh https://raw.githubusercontent.com/teddysun/shadowsocks ...
- vue.js嵌套路由-------由浅入深
嵌套路由就是路由里面嵌套他的子路由 子路由关键属性children 每一个子路由里面可以嵌套多个组件 子组件又有路由导航和路由容器 <router-link to="/父路由的地址名字 ...
- CODEVS1222 信与信封问题 (匈牙利算法)
先做一遍匈牙利算法.对于已经匹配的边,如果删去之后还能最大匹配数增加,则不符合要求. 一遍匈牙利算法是O(n^3)的,对于每一条边做n次,每次O(n^2),总的复杂度是O(n^3). 注意:不要忘记输 ...
- 20181010关于pt-kill自动杀死运行超长的进程
转自: http://blog.chinaunix.net/uid-16844903-id-4442030.htmlhttp://blog.chinaunix.net/uid-31396856-id- ...
- Oracle 11.2.0.4.0安装
http://opensgalaxy.com/2015/08/25/oracle11-2-0-4-0%E5%AE%89%E8%A3%85%E5%8F%8A%E8%A1%A5%E4%B8%81%E8%8 ...