18720358503 在线客服 人才招聘 返回顶部
企业动态 技术分享 行业动态

有关怎样完成FTP提交或是免费下载带进展和速度

2020-12-30分享 "> 对不起,没有下一图集了!">

有关怎样完成FTP提交或是免费下载带进展和速度的完成方式


小视频,自媒体平台,达种族草一站服务

在这里里必须表明的是,该方法是根据别的编码开展改善的。

最先大家必须界定一个授权委托,用于完成传送全过程中国传媒大学递文档的数量,完成的字节数数和速率,便捷顾客端页面上涨用。

public delegate void TransferProcess(long total,long finished,double speed);

启用编码也不举例说明了

接下去大家创建一个FTPClient类,该类根据socket和FTP协议书完成了联接FTP服务,创建文件目录,提交文档,免费下载文档等关键方式。构造以下:

 

必须留意的是,大家必须定一个恶性事件event TransferProcess OnTransferProcess;该恶性事件在案例化FTPClient以后必须启用,这一恶性事件对完成进展条和速度是是非非常关键的。以便完成速度大家还必须界定个公布的组员startTime(刚开始時间)。大家如今关键是看一下怎样提交的。

///

/// 提交一个文档

///

/// 当地文档名

public void Put(string strFileName)

{

//联接网络服务器

if (!bConnected)

{

Connect();

}

UpdateStatus = true;

//创建socket联接

Socket socketData = CreateDataSocket();

//向FTP网络服务器产生储存指令

SendCommand( STOR + Path.GetFileName(strFileName));

//怎样网络服务器回到的信息内容并不是大家需要要的,就抛出去出现异常

if (!(iReplyCode == 125 || iReplyCode == 150))

{

throw new IOException(strReply.Substring(4));

}

//创建当地文档的数据信息流

FileStream input = new

FileStream(strFileName, FileMode.Open);

int iBytes = 0;

long total = input.Length;//该组员关键纪录文档的总字节数数,留意这儿应用长整型,是以便提升只有传送2G上下的文档的限定

long finished = 0;//该组员关键纪录早已传送进行的字节数数,留意这儿应用长整型,是以便提升只有传送2G上下的文档的限定

double speed = 0;//纪录传送的速度

while ((iBytes = input.Read(buffer, 0, buffer.Length)) 0)//循环系统从当地数据信息流中载入数据信息到缓存区

{

//Console.WriteLine(startTime.ToString());

socketData.Send(buffer, iBytes, 0);//将缓存区的数据信息推送到FTP网络服务器

DateTime endTime = DateTime.Now;//每一次推送数据信息的完毕時间

TimeSpan ts = endTime - startTime;//测算每一次推送数据信息的時间间距

finished += iBytes;//测算进行的字节数数.

Console.WriteLine(ts.Milliseconds);

//测算速度,留意finished是字节数,因此必须计算冲K字节数

if (ts.Milliseconds 0)

{

speed = (double)(finished / ts.TotalMilliseconds);

speed = Math.Round(speed * 1000 / 1024, 2);

}

//这儿是不可或缺的,不然你没法完成进展条

//假如传送进展恶性事件被案例化,并且从当地数据信息流中载入数据信息并不是空的并进行的字节数数都不为空得话,则完成授权委托.

if (OnTransferProcess != null iBytes 0 finished 0)

{

OnTransferProcess(total, finished,speed);

}

}

UpdateStatus = false;

finished = 0;

input.Close();//当传送进行以后必须关掉数据信息流,便于下一次浏览.

if (socketData.Connected)

{

socketData.Close();//关掉当今的socket

}

if (!(iReplyCode == 226 || iReplyCode == 250))

{

ReadReply();

if (!(iReplyCode == 226 || iReplyCode == 250))

{

UpdateStatus = false;

throw new IOException(strReply.Substring(4));

}

}

}

上边编码中注解写的较为详尽,这儿也不再逐一解读了,有关免费下载中完成进展条和速度的难题能够参照之上编码开展改动.

详细的编码以下:

using System;

using System;

using System.IO;

using System.Text;

using System.Sockets;

namespace MMSEncoder

{

public delegate void TransferProcess(long total,long finished,double speed);

///

/// FTP Client

///

public class FTPClient

{

public event TransferProcess OnTransferProcess;

public bool UpdateStatus = true;

public DateTime startTime;

private bool IsAbortConnect = false;

#region 结构涵数

///

/// 默认设置结构涵数

///

public FTPClient()

{

strRemoteHost =

strRemotePath =

strRemoteUser =

strRemotePass =

strRemotePort = 21;

bConnected = false;

}

///

/// 结构涵数

///

/// FTP网络服务器IP详细地址

/// 当今网络服务器文件目录

/// 登陆客户账户

/// 登陆客户登陆密码

/// FTP网络服务器端口号

public FTPClient(string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort)

{

strRemoteHost = remoteHost;

strRemotePath = remotePath;

strRemoteUser = remoteUser;

strRemotePass = remotePass;

strRemotePort = remotePort;

Connect();

}

#endregion

#region 登录字段名、特性

///

/// FTP网络服务器IP详细地址

///

private string strRemoteHost;

public string RemoteHost

{

get

{

return strRemoteHost;

}

set

{

strRemoteHost = value;

}

}

///

/// FTP网络服务器端口号

///

private int strRemotePort;

public int RemotePort

{

get

{

return strRemotePort;

}

set

{

strRemotePort = value;

}

}

///

/// 当今网络服务器文件目录

///

private string strRemotePath;

public string RemotePath

{

get

{

return strRemotePath;

}

set

{

strRemotePath = value;

}

}

///

/// 登陆客户账户

///

private string strRemoteUser;

public string RemoteUser

{

set

{

strRemoteUser = value;

}

}

///

/// 客户登陆登陆密码

///

private string strRemotePass;

public string RemotePass

{

set

{

strRemotePass = value;

}

}

///

/// 是不是登陆

///

private Boolean bConnected;

public bool Connected

{

get

{

return bConnected;

}

}

#endregion

#region 连接

///

/// 创建联接

///

public void Connect()

{

//if (IsAbortConnect) throw new IOException( 客户强制性停止了FTP

socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);

// 连接

try

{

socketControl.Connect(ep);

}

catch (Exception)

{

throw new IOException( 没法联接到远程控制网络服务器!

}

// 获得回复码

ReadReply();

if (iReplyCode != 220)

{

DisConnect();

throw new IOException(strReply.Substring(4));

}

// 登录

SendCommand( USER + strRemoteUser);

if (!(iReplyCode == 331 || iReplyCode == 230))

{

CloseSocketConnect();//关掉联接

throw new IOException(strReply.Substring(4));

}

if (iReplyCode != 230)

{

SendCommand( PASS + strRemotePass);

if (!(iReplyCode == 230 || iReplyCode == 202))

{

CloseSocketConnect();//关掉联接

throw new IOException(strReply.Substring(4));

}

}

bConnected = true;

// 转换到原始文件目录

if (!string.IsNullOrEmpty(strRemotePath))

{

ChDir(strRemotePath);

}

}

///

/// 关掉联接

///

public void DisConnect()

{

if (socketControl != null)

{

SendCommand( QUIT

}

CloseSocketConnect();

}

public void AbortConnect()

{

if (socketControl != null)

{

SendCommand( ABOR

}

IsAbortConnect = true;

//CloseSocketConnect();

}

#endregion

#region 传送方式

///

/// 传送方式:二进制种类、ASCII种类

///

public enum TransferType { Binary, ASCII };

///

/// 设定传送方式

///

/// 传送方式

public void SetTransferType(TransferType ttType)

{

if (ttType == TransferType.Binary)

{

SendCommand( TYPE I //binary种类传送

}

else

{

SendCommand( TYPE A //ASCII种类传送

}

if (iReplyCode != 200)

{

throw new IOException(strReply.Substring(4));

}

else

{

trType = ttType;

}

}

///

/// 得到传送方式

///

/// 传送方式

public TransferType GetTransferType()

{

return trType;

}

#endregion

#region 文档实际操作

///

/// 得到文档目录

///

/// 文档名的配对标识符串

///

public string[] Dir(string strMask)

{

// 创建连接

if (!bConnected)

{

Connect();

}

//创建开展数据信息联接的socket

Socket socketData = CreateDataSocket();

//传输指令

SendCommand( NLST + strMask);

//剖析回复编码

if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))

{

throw new IOException(strReply.Substring(4));

}

//得到結果

strMsg =

while (true)

{

int iBytes = socketData.Receive(buffer, buffer.Length, 0);

strMsg += GB2312.GetString(buffer, 0, iBytes);

if (iBytes buffer.Length)

{

break;

}

}

char[] seperator = { '\n' };

string[] strsFileList = strMsg.Split(seperator);

socketData.Close();//数据信息socket关掉时也会出现回到码

if (iReplyCode != 226)

{

ReadReply();

if (iReplyCode != 226)

{

throw new IOException(strReply.Substring(4));

}

}

return strsFileList;

}

///

/// 获得文档尺寸

///

/// 文档名

/// 文档尺寸

public long GetFileSize(string strFileName)

{

if (!bConnected)

{

Connect();

}

SendCommand( SIZE + Path.GetFileName(strFileName));

long lSize = 0;

if (iReplyCode == 213)

{

lSize = Int64.Parse(strReply.Substring(4));

}

else

{

throw new IOException(strReply.Substring(4));

}

return lSize;

}

///

/// 删掉

///

/// 待删掉文档名

public void Delete(string strFileName)

{

if (!bConnected)

{

Connect();

}

SendCommand( DELE + strFileName);

if (iReplyCode != 250)

{

throw new IOException(strReply.Substring(4));

}

}

///

/// 重新命名(假如新文档名与现有文档重名,将遮盖现有文档)

///

/// 旧文档名

/// 新文档名

public void Rename(string strOldFileName, string strNewFileName)

{

if (!bConnected)

{

Connect();

}

SendCommand( RNFR + strOldFileName);

if (iReplyCode != 350)

{

throw new IOException(strReply.Substring(4));

}

// 假如新文档名与原来文档重名,将遮盖原来文档

SendCommand( RNTO + strNewFileName);

if (iReplyCode != 250)

{

throw new IOException(strReply.Substring(4));

}

}

#endregion

#region 提交和免费下载

///

/// 免费下载一批件件

///

/// 文档名的配对标识符串

/// 当地文件目录(不可以\完毕)

public void Get(string strFileNameMask, string strFolder)

{

if (!bConnected)

{

Connect();

}

string[] strFiles = Dir(strFileNameMask);

foreach (string strFile in strFiles)

{

if (!strFile.Equals( ))//一般来讲strFiles的最终一个原素将会是空标识符串

{

if (strFile.LastIndexOf( . ) -1)

{

Get(strFile.Replace( \r , ), strFolder, strFile.Replace( \r , ));

}

}

}

}

///

/// 免费下载一个文档

///

/// 要免费下载的文档名

/// 当地文件目录(不可以\完毕)

/// 储存在当地时的文档名

public void Get(string strRemoteFileName, string strFolder, string strLocalFileName)

{

if (!bConnected)

{

Connect();

}

SetTransferType(TransferType.Binary);

if (strLocalFileName.Equals( ))

{

strLocalFileName = strRemoteFileName;

}

if (!File.Exists(strLocalFileName))

{

Stream st = File.Create(strLocalFileName);

st.Close();

}

FileStream output = new

FileStream(strFolder + \\ + strLocalFileName, FileMode.Create);

Socket socketData = CreateDataSocket();

SendCommand( RETR + strRemoteFileName);

if (!(iReplyCode == 150 || iReplyCode == 125

|| iReplyCode == 226 || iReplyCode == 250))

{

throw new IOException(strReply.Substring(4));

}

while (true)

{

int iBytes = socketData.Receive(buffer, buffer.Length, 0);

output.Write(buffer, 0, iBytes);

if (iBytes = 0)

{

break;

}

}

output.Close();

if (socketData.Connected)

{

socketData.Close();

}

if (!(iReplyCode == 226 || iReplyCode == 250))

{

ReadReply();

if (!(iReplyCode == 226 || iReplyCode == 250))

{

throw new IOException(strReply.Substring(4));

}

}

}

///

/// 提交一批件件

///

/// 当地文件目录(不可以\完毕)

/// 文档名配对标识符(能够包括*和?)

public void Put(string strFolder, string strFileNameMask)

{

string[] strFiles = Directory.GetFiles(strFolder, strFileNameMask);

foreach (string strFile in strFiles)

{

//strFile是详细的文档名(包括相对路径)

Put(strFile);

}

}

///

/// 提交一个文档

///

/// 当地文档名

public void Put(string strFileName)

{

if (!bConnected)

{

Connect();

}

UpdateStatus = true;

Socket socketData = CreateDataSocket();

SendCommand( STOR + Path.GetFileName(strFileName));

if (!(iReplyCode == 125 || iReplyCode == 150))

{

throw new IOException(strReply.Substring(4));

}

FileStream input = new

FileStream(strFileName, FileMode.Open);

int iBytes = 0;

long total = input.Length;

long finished = 0;

//DateTime startTime = DateTime.Now;

double speed = 0;

while ((iBytes = input.Read(buffer, 0, buffer.Length)) 0)

{

Console.WriteLine(startTime.ToString());

socketData.Send(buffer, iBytes, 0);

DateTime endTime = DateTime.Now;

TimeSpan ts = endTime - startTime;

finished += iBytes;

Console.WriteLine(ts.Milliseconds);

if (ts.Milliseconds 0)

{

speed = (double)(finished / ts.TotalMilliseconds);

speed = Math.Round(speed * 1000 / 1024, 2);

}

if (OnTransferProcess != null iBytes 0 finished 0)

{

OnTransferProcess(total, finished,speed);

}

}

UpdateStatus = false;

finished = 0;

input.Close();

if (socketData.Connected)

{

socketData.Close();

}

if (!(iReplyCode == 226 || iReplyCode == 250))

{

ReadReply();

if (!(iReplyCode == 226 || iReplyCode == 250))

{

UpdateStatus = false;

throw new IOException(strReply.Substring(4));

}

}

}

#endregion

#region 文件目录实际操作

///

/// 建立文件目录

///

/// 文件目录名

public void MkDir(string strDirName)

{

if (!bConnected)

{

Connect();

}

SendCommand( MKD + strDirName);

if (iReplyCode != 257)

{

throw new IOException(strReply.Substring(4));

}

}

///

/// 删掉文件目录

///

/// 文件目录名

public void RmDir(string strDirName)

{

if (!bConnected)

{

Connect();

}

SendCommand( RMD + strDirName);

if (iReplyCode != 250)

{

throw new IOException(strReply.Substring(4));

}

}

///

/// 更改文件目录

///

/// 新的工作中文件目录名

public void ChDir(string strDirName)

{

if (strDirName.Equals( . ) || strDirName.Equals( ))

{

return;

}

if (!bConnected)

{

Connect();

}

SendCommand( CWD + strDirName);

if (iReplyCode != 250)

{

throw new IOException(strReply.Substring(4));

}

this.strRemotePath = strDirName;

}

#endregion

#region 內部自变量

///

/// 网络服务器回到的回复信息内容(包括回复码)

///

private string strMsg;

///

/// 网络服务器回到的回复信息内容(包括回复码)

///

private string strReply;

///

/// 网络服务器回到的回复码

///

private int iReplyCode;

///

/// 开展操纵联接的socket

///

private Socket socketControl;

///

/// 传送方式

///

private TransferType trType;

///

/// 接受和推送数据信息的缓存区

///

private static int BLOCK_SIZE = Int16.MaxValue;

Byte[] buffer = new Byte[BLOCK_SIZE];

///

/// 编号方法(为避免出现汉语错码选用 GB2312编号方法)

///

Encoding GB2312 = Encoding.Default ;//Encoding.GetEncoding( gb2312

#endregion

#region 內部涵数

///

/// 将一行回复标识符串纪录在strReply和strMsg

/// 回复码纪录在iReplyCode

///

private void ReadReply()

{

strMsg =

strReply = ReadLine();

iReplyCode = Int32.Parse(strReply.Substring(0, 3));

}

///

/// 创建开展数据信息联接的socket

///

/// 数据信息联接socket

private Socket CreateDataSocket()

{

SendCommand( PASV

if (iReplyCode != 227)

{

throw new IOException(strReply.Substring(4));

}

int index1 = strReply.IndexOf('(');

int index2 = strReply.IndexOf(')');

string ipData =

strReply.Substring(index1 + 1, index2 - index1 - 1);

int[] parts = new int[6];

int len = ipData.Length;

int partCount = 0;

string buf =

for (int i = 0; i len partCount i++)

{

char ch = Char.Parse(ipData.Substring(i, 1));

if (Char.IsDigit(ch))

buf += ch;

else if (ch != ',')

{

throw new IOException( Malformed PASV strReply: +

strReply);

}

if (ch == ',' || i + 1 == len)

{

try

{

parts[partCount++] = Int32.Parse(buf);

buf =

}

catch (Exception)

{

throw new IOException( Malformed PASV strReply: +

strReply);

}

}

}

string ipAddress = parts[0] + . + parts[1] + . +

parts[2] + . + parts[3];

int port = (parts[4] 8) + parts[5];

Socket s = new

Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPEndPoint ep = new

IPEndPoint(IPAddress.Parse(ipAddress), port);

try

{

s.Connect(ep);

}

catch (Exception)

{

throw new IOException( 没法联接网络服务器

}

return s;

}

///

/// 关掉socket联接(用以登陆之前)

///

private void CloseSocketConnect()

{

if (socketControl != null)

{

socketControl.Close();

socketControl = null;

}

bConnected = false;

}

///

/// 载入Socket回到的全部标识符串

///

/// 包括回复码的标识符串行通信

private string ReadLine()

{

while (true)

{

int iBytes = socketControl.Receive(buffer, buffer.Length, 0);

strMsg += GB2312.GetString(buffer, 0, iBytes);

if (iBytes buffer.Length)

{

break;

}

}

char[] seperator = { '\n' };

string[] mess = strMsg.Split(seperator);

if (strMsg.Length 2)

{

strMsg = mess[mess.Length - 2];

//seperator[0]是10,换行符是由13和0构成的,隔开后10后边虽沒有标识符串,

//但也会分派为空标识符串给后边(也是最终一个)标识符串数字能量数组,

//因此最终一个mess是不起作用的空标识符串

//但为何不立即取mess[0],由于仅有最终一行标识符串回复码与信息内容中间有时间格

}

else

{

strMsg = mess[0];

}

if (!strMsg.Substring(3, 1).Equals( ))//回到标识符串恰当的是以回复码(如220开始,后边接一空格符,再接问好标识符串)

{

return ReadLine();

}

return strMsg;

}

///

/// 推送指令并获得回复码和最终一行回复标识符串

///

/// 指令

private void SendCommand(String strCommand)

{

Byte[] cmdBytes =

GB2312.GetBytes((strCommand + \r\n ).ToCharArray());

socketControl.Send(cmdBytes, cmdBytes.Length, 0);

ReadReply();

}

#endregion

}

}


"> 对不起,没有下一图集了!">
在线咨询