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. 首先看下上传页 ...
随机推荐
- Oracle 控制文件(CONTROLFILE)
一.Oracle 控制文件 为二进制文件,初始化大小由CREATE DATABASE指定,可以使用RMAN备份 记录了当前数据库的结构信息,同时也包含数据文件及日志文件的信息以及相关的状态,归档信息等 ...
- C语言中指针的加减运算
参考文章,值得一看 char arr[3]; printf("arr:\n%d\n%d\n%d\n", arr, arr + 1, arr + 2); char *parr[3]; ...
- Servlet过滤器的使用
Servlet过滤器的使用 制作人:全心全意 Servlet过滤器:Servlet过滤器与Servlet十分相似,但它具有拦截客户端请求的功能,Servlet过滤器可以改变请求中的内容,来满足实际开发 ...
- Spring 源码学习(一)
工作好多年了,越来越心浮气躁了,好多东西都是一知半解的,所以现在需要静下心来好好学习一门技术. 就选Spring了, spring 设计java 开发的方方面面. 期待目标 对Spring 有个更深层 ...
- String类的获取功能
/* * String类的获取功能: * int length():获取字符串的长度,其实也就是字符个数 * char charAt(int index):获取指定索引处的字符 * int index ...
- Maven学习总结(32)——Maven项目部署到Tomcat8中
1.环境准备 Maven.Tomcat8.Eclipse 2.maven中的镜像配置 大家知道,mavne默认使用的是国外的镜像,但是速度很慢,这里建议大家使用阿里的中央仓库镜像. 阿里出品,必出精品 ...
- 【Google Chrome】Google Chrome快捷键大全
相信很多朋友在使用过Google Chrome之后,就会不想回到原先使用的浏览器了,尤其是IE.没错Google Chrome的优点很多,已经获得了一大部分网友们的用户,软件志现在也是Firefox+ ...
- - > 动规讲解基础讲解一——01背包(模板)
作为动态规划的基础,01背包的思想在许多动规问题中会经常出现,so,熟练的掌握01背包的思路是极其重要的: 有n件物品,第i件物品(I = 1,2,3…n)的价值是vi, 重量是wi,我们有一个能承重 ...
- Ubuntu查看系统版本的方法
1. less /etc/issue 2. less /proc/version 3. uname -a 4. lsb_release -a
- Windows 10+Ubuntu 16.04双系统切换后时间不同步的问题解决
主要时时区不对造成的,少了8个小时,解决方法: 在Ubuntu的终端下输入以下命令: sudo timedatectl set-local-rtc 1 参考: http://blog.csdn.net ...