文档说明
本文档使用socket通信方式来实现ftp文件的上传下载等命令的执行
1.基本介绍
由于最近的项目是客户端的程序,需要将客户端的图片文件【切图】-【打包】-【ftp上传】,现在就差最后一步了,慢慢的把这些小功能实现了,合并到一起就是一个大功能了,所以一个业务需要拆分的很小很小才可以看清楚,这个项目实际需要用到哪些知识点,下面介绍一下ftp上传的命令
ftp命令的参考链接:
ftp适合小文件上传
对带宽要求要求较高
服务器安全性也要考虑到
命令需要熟悉,不然比较难
2.实际项目
文件上传
文件下载
删除文件
创建文件夹
文件夹重命名
删除文件夹
改变目录
获取文件夹中文件列表
等等
2.1 图片上传和下载

写了几个方法,一般用的最多的就是put,具体的可以下载复制源码下来进行实战一下。
2.2 目录创建和删除

这个方法今天刚好用上了,折腾了一会,才搞定的。
3.调用代码参考
由于这个帮助类不是静态的,所以需要实例化
?| 1 2 3 |
string username = "xxx";
string password = "xxx";
var ftp = new ftpclienthelper("xxx", ".", username, password, 1021);
|
下面还是调用常用的方法,就可以了,因为账号、密码、服务器的ip地址都被我用“xxx”代替了,所以大家自己改下,还有ftp默认端口号是:1021,如果有变动还是需要自己改下的。
4.ftpclienthelper下载
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 |
//-------------------------------------------------------------------------------------
// all rights reserved , copyright (c) 2015 , zto , ltd .
//-------------------------------------------------------------------------------------
using system;
using system.io;
using system.net;
using system.net.sockets;
using system.text;
using system.threading;
namespace zto.pictest.utilities
{
/// <summary>
/// ftp操作帮助类
///
/// 修改纪录
///
/// 2016-4-4 版本:1.0 yanghenglian 创建主键,注意命名空间的排序,测试非常好。
///
/// 版本:1.0
///
/// <author>
/// <name>yanghenglian</name>
/// <date>2016-4-4</date>
/// </author>
/// </summary>
public class ftpclienthelper
{
public static object obj = new object();
#region 构造函数
/// <summary>
/// 缺省构造函数
/// </summary>
public ftpclienthelper()
{
remotehost = "";
_strremotepath = "";
_strremoteuser = "";
_strremotepass = "";
_strremoteport = 21;
_bconnected = false;
}
/// <summary>
/// 构造函数
/// </summary>
public ftpclienthelper(string remotehost, string remotepath, string remoteuser, string remotepass, int remoteport)
{
// ip地址
remotehost = remotehost;
// 这个很重要,表示连接路径,如果是.表示根目录
_strremotepath = remotepath;
// 登录账号
_strremoteuser = remoteuser;
// 登录密码
_strremotepass = remotepass;
// ftp端口号
_strremoteport = remoteport;
connect();
}
#endregion
#region 字段
private int _strremoteport;
private boolean _bconnected;
private string _strremotepass;
private string _strremoteuser;
private string _strremotepath;
/// <summary>
/// 服务器返回的应答信息(包含应答码)
/// </summary>
private string _strmsg;
/// <summary>
/// 服务器返回的应答信息(包含应答码)
/// </summary>
private string _strreply;
/// <summary>
/// 服务器返回的应答码
/// </summary>
private int _ireplycode;
/// <summary>
/// 进行控制连接的socket
/// </summary>
private socket _socketcontrol;
/// <summary>
/// 传输模式
/// </summary>
private transfertype _trtype;
/// <summary>
/// 接收和发送数据的缓冲区
/// </summary>
private const int blocksize = 512;
/// <summary>
/// 编码方式
/// </summary>
readonly encoding _ascii = encoding.ascii;
/// <summary>
/// 字节数组
/// </summary>
readonly byte[] _buffer = new byte[blocksize];
#endregion
#region 属性
/// <summary>
/// ftp服务器ip地址
/// </summary>
public string remotehost { get; set; }
/// <summary>
/// ftp服务器端口
/// </summary>
public int remoteport
{
get
{
return _strremoteport;
}
set
{
_strremoteport = value;
}
}
/// <summary>
/// 当前服务器目录
/// </summary>
public string remotepath
{
get
{
return _strremotepath;
}
set
{
_strremotepath = value;
}
}
/// <summary>
/// 登录用户账号
/// </summary>
public string remoteuser
{
set
{
_strremoteuser = value;
}
}
/// <summary>
/// 用户登录密码
/// </summary>
public string remotepass
{
set
{
_strremotepass = value;
}
}
/// <summary>
/// 是否登录
/// </summary>
public bool connected
{
get
{
return _bconnected;
}
}
#endregion
#region 链接
/// <summary>
/// 建立连接
/// </summary>
public void connect()
{
lock (obj)
{
_socketcontrol = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
var ep = new ipendpoint(ipaddress.parse(remotehost), _strremoteport);
try
{
_socketcontrol.connect(ep);
}
catch (exception)
{
throw new ioexception("不能连接ftp服务器");
}
}
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;
chdir(_strremotepath);
}
/// <summary>
/// 关闭连接
/// </summary>
public void disconnect()
{
if (_socketcontrol != null)
{
sendcommand("quit");
}
closesocketconnect();
}
#endregion
#region 传输模式
/// <summary>
/// 传输模式:二进制类型、ascii类型
/// </summary>
public enum transfertype { binary, ascii };
/// <summary>
/// 设置传输模式
/// </summary>
/// <param name="tttype">传输模式</param>
public void settransfertype(transfertype tttype)
{
sendcommand(tttype == transfertype.binary ? "type i" : "type a");
if (_ireplycode != 200)
{
throw new ioexception(_strreply.substring(4));
}
_trtype = tttype;
}
/// <summary>
/// 获得传输模式
/// </summary>
/// <returns>传输模式</returns>
public transfertype gettransfertype()
{
return _trtype;
}
#endregion
#region 文件操作
/// <summary>
/// 获得文件列表
/// </summary>
/// <param name="strmask">文件名的匹配字符串</param>
public string[] dir(string strmask)
{
if (!_bconnected)
{
connect();
}
socket socketdata = createdatasocket();
sendcommand("nlst " + strmask);
if (!(_ireplycode == 150 || _ireplycode == 125 || _ireplycode == 226))
{
throw new ioexception(_strreply.substring(4));
}
_strmsg = "";
thread.sleep(2000);
while (true)
{
int ibytes = socketdata.receive(_buffer, _buffer.length, 0);
_strmsg += _ascii.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 void newputbyguid(string strfilename, string strguid)
{
if (!_bconnected)
{
connect();
}
string str = strfilename.substring(0, strfilename.lastindexof("\\", stringcomparison.ordinal));
string strtypename = strfilename.substring(strfilename.lastindexof(".", stringcomparison.ordinal));
strguid = str + "\\" + strguid;
socket socketdata = createdatasocket();
sendcommand("stor " + path.getfilename(strguid));
if (!(_ireplycode == 125 || _ireplycode == 150))
{
throw new ioexception(_strreply.substring(4));
}
var input = new filestream(strguid, filemode.open);
input.flush();
int ibytes;
while ((ibytes = input.read(_buffer, 0, _buffer.length)) > 0)
{
socketdata.send(_buffer, ibytes, 0);
}
input.close();
if (socketdata.connected)
{
socketdata.close();
}
if (!(_ireplycode == 226 || _ireplycode == 250))
{
readreply();
if (!(_ireplycode == 226 || _ireplycode == 250))
{
throw new ioexception(_strreply.substring(4));
}
}
}
/// <summary>
/// 获取文件大小
/// </summary>
/// <param name="strfilename">文件名</param>
/// <returns>文件大小</returns>
public long getfilesize(string strfilename)
{
if (!_bconnected)
{
connect();
}
sendcommand("size " + path.getfilename(strfilename));
long lsize;
if (_ireplycode == 213)
{
lsize = int64.parse(_strreply.substring(4));
}
else
{
throw new ioexception(_strreply.substring(4));
}
return lsize;
}
/// <summary>
/// 获取文件信息
/// </summary>
/// <param name="strfilename">文件名</param>
/// <returns>文件大小</returns>
public string getfileinfo(string strfilename)
{
if (!_bconnected)
{
connect();
}
socket socketdata = createdatasocket();
sendcommand("list " + strfilename);
if (!(_ireplycode == 150 || _ireplycode == 125
|| _ireplycode == 226 || _ireplycode == 250))
{
throw new ioexception(_strreply.substring(4));
}
byte[] b = new byte[512];
memorystream ms = new memorystream();
while (true)
{
int ibytes = socketdata.receive(b, b.length, 0);
ms.write(b, 0, ibytes);
if (ibytes <= 0)
{
break;
}
}
byte[] bt = ms.getbuffer();
string strresult = encoding.ascii.getstring(bt);
ms.close();
return strresult;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="strfilename">待删除文件名</param>
public void delete(string strfilename)
{
if (!_bconnected)
{
connect();
}
sendcommand("dele " + strfilename);
if (_ireplycode != 250)
{
throw new ioexception(_strreply.substring(4));
}
}
/// <summary>
/// 重命名(如果新文件名与已有文件重名,将覆盖已有文件)
/// </summary>
/// <param name="stroldfilename">旧文件名</param>
/// <param name="strnewfilename">新文件名</param>
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 上传和下载
/// <summary>
/// 下载一批文件
/// </summary>
/// <param name="strfilenamemask">文件名的匹配字符串</param>
/// <param name="strfolder">本地目录(不得以\结束)</param>
public void get(string strfilenamemask, string strfolder)
{
if (!_bconnected)
{
connect();
}
string[] strfiles = dir(strfilenamemask);
foreach (string strfile in strfiles)
{
if (!strfile.equals(""))//一般来说strfiles的最后一个元素可能是空字符串
{
get(strfile, strfolder, strfile);
}
}
}
/// <summary>
/// 下载一个文件
/// </summary>
/// <param name="strremotefilename">要下载的文件名</param>
/// <param name="strfolder">本地目录(不得以\结束)</param>
/// <param name="strlocalfilename">保存在本地时的文件名</param>
public void get(string strremotefilename, string strfolder, string strlocalfilename)
{
socket socketdata = createdatasocket();
try
{
if (!_bconnected)
{
connect();
}
settransfertype(transfertype.binary);
if (strlocalfilename.equals(""))
{
strlocalfilename = strremotefilename;
}
sendcommand("retr " + strremotefilename);
if (!(_ireplycode == 150 || _ireplycode == 125 || _ireplycode == 226 || _ireplycode == 250))
{
throw new ioexception(_strreply.substring(4));
}
var output = new filestream(strfolder + "\\" + strlocalfilename, filemode.create);
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));
}
}
}
catch
{
socketdata.close();
_socketcontrol.close();
_bconnected = false;
_socketcontrol = null;
}
}
/// <summary>
/// 下载一个文件
/// </summary>
/// <param name="strremotefilename">要下载的文件名</param>
/// <param name="strfolder">本地目录(不得以\结束)</param>
/// <param name="strlocalfilename">保存在本地时的文件名</param>
public void getnobinary(string strremotefilename, string strfolder, string strlocalfilename)
{
if (!_bconnected)
{
connect();
}
if (strlocalfilename.equals(""))
{
strlocalfilename = strremotefilename;
}
socket socketdata = createdatasocket();
sendcommand("retr " + strremotefilename);
if (!(_ireplycode == 150 || _ireplycode == 125 || _ireplycode == 226 || _ireplycode == 250))
{
throw new ioexception(_strreply.substring(4));
}
var output = new filestream(strfolder + "\\" + strlocalfilename, filemode.create);
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));
}
}
}
/// <summary>
/// 上传一批文件
/// </summary>
/// <param name="strfolder">本地目录(不得以\结束)</param>
/// <param name="strfilenamemask">文件名匹配字符(可以包含*和?)</param>
public void put(string strfolder, string strfilenamemask)
{
string[] strfiles = directory.getfiles(strfolder, strfilenamemask);
foreach (string strfile in strfiles)
{
put(strfile);
}
}
/// <summary>
/// 上传一个文件
/// </summary>
/// <param name="strfilename">本地文件名</param>
public void put(string strfilename)
{
if (!_bconnected)
{
connect();
}
socket socketdata = createdatasocket();
if (path.getextension(strfilename) == "")
sendcommand("stor " + path.getfilenamewithoutextension(strfilename));
else
sendcommand("stor " + path.getfilename(strfilename));
if (!(_ireplycode == 125 || _ireplycode == 150))
{
throw new ioexception(_strreply.substring(4));
}
var input = new filestream(strfilename, filemode.open);
int ibytes;
while ((ibytes = input.read(_buffer, 0, _buffer.length)) > 0)
{
socketdata.send(_buffer, ibytes, 0);
}
input.close();
if (socketdata.connected)
{
socketdata.close();
}
if (!(_ireplycode == 226 || _ireplycode == 250))
{
readreply();
if (!(_ireplycode == 226 || _ireplycode == 250))
{
throw new ioexception(_strreply.substring(4));
}
}
}
/// <summary>
/// 上传一个文件
/// </summary>
/// <param name="strfilename">本地文件名</param>
///
/// <param name="strguid"> </param>
public void putbyguid(string strfilename, string strguid)
{
if (!_bconnected)
{
connect();
}
string str = strfilename.substring(0, strfilename.lastindexof("\\", stringcomparison.ordinal));
string strtypename = strfilename.substring(strfilename.lastindexof(".", system.stringcomparison.ordinal));
strguid = str + "\\" + strguid;
file.copy(strfilename, strguid);
file.setattributes(strguid, fileattributes.normal);
socket socketdata = createdatasocket();
sendcommand("stor " + path.getfilename(strguid));
if (!(_ireplycode == 125 || _ireplycode == 150))
{
throw new ioexception(_strreply.substring(4));
}
var input = new filestream(strguid, filemode.open, system.io.fileaccess.read, system.io.fileshare.read);
int ibytes = 0;
while ((ibytes = input.read(_buffer, 0, _buffer.length)) > 0)
{
socketdata.send(_buffer, ibytes, 0);
}
input.close();
file.delete(strguid);
if (socketdata.connected)
{
socketdata.close();
}
if (!(_ireplycode == 226 || _ireplycode == 250))
{
readreply();
if (!(_ireplycode == 226 || _ireplycode == 250))
{
throw new ioexception(_strreply.substring(4));
}
}
}
#endregion
#region 目录操作
/// <summary>
/// 创建目录
/// </summary>
/// <param name="strdirname">目录名</param>
public void mkdir(string strdirname)
{
if (!_bconnected)
{
connect();
}
sendcommand("mkd " + strdirname);
if (_ireplycode != 257)
{
throw new ioexception(_strreply.substring(4));
}
}
/// <summary>
/// 删除目录
/// </summary>
/// <param name="strdirname">目录名</param>
public void rmdir(string strdirname)
{
if (!_bconnected)
{
connect();
}
sendcommand("rmd " + strdirname);
if (_ireplycode != 250)
{
throw new ioexception(_strreply.substring(4));
}
}
/// <summary>
/// 改变目录
/// </summary>
/// <param name="strdirname">新的工作目录名</param>
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 内部函数
/// <summary>
/// 将一行应答字符串记录在strreply和strmsg,应答码记录在ireplycode
/// </summary>
private void readreply()
{
_strmsg = "";
_strreply = readline();
_ireplycode = int32.parse(_strreply.substring(0, 3));
}
/// <summary>
/// 建立进行数据连接的socket
/// </summary>
/// <returns>数据连接socket</returns>
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 <= 6; 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];
var s = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
var ep = new ipendpoint(ipaddress.parse(ipaddress), port);
try
{
s.connect(ep);
}
catch (exception)
{
throw new ioexception("无法连接ftp服务器");
}
return s;
}
/// <summary>
/// 关闭socket连接(用于登录以前)
/// </summary>
private void closesocketconnect()
{
lock (obj)
{
if (_socketcontrol != null)
{
_socketcontrol.close();
_socketcontrol = null;
}
_bconnected = false;
}
}
/// <summary>
/// 读取socket返回的所有字符串
/// </summary>
/// <returns>包含应答码的字符串行</returns>
private string readline()
{
lock (obj)
{
while (true)
{
int ibytes = _socketcontrol.receive(_buffer, _buffer.length, 0);
_strmsg += _ascii.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];
}
else
{
_strmsg = mess[0];
}
if (!_strmsg.substring(3, 1).equals(" ")) //返回字符串正确的是以应答码(如220开头,后面接一空格,再接问候字符串)
{
return readline();
}
return _strmsg;
}
/// <summary>
/// 发送命令并获取应答码和最后一行应答字符串
/// </summary>
/// <param name="strcommand">命令</param>
public void sendcommand(string strcommand)
{
lock (obj)
{
byte[] cmdbytes = encoding.ascii.getbytes((strcommand + "\r\n").tochararray());
_socketcontrol.send(cmdbytes, cmdbytes.length, 0);
thread.sleep(100);
readreply();
}
}
#endregion
}
}
|
5.ftp常用的命令
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
#region 程序集 system.dll, v4.0.0.0
// c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\system.dll
#endregion
using system;
namespace system.net
{
// 摘要:
// system.net.webrequestmethods.ftp、system.net.webrequestmethods.file 和 system.net.webrequestmethods.http
// 类的容器类。无法继承此类
public static class webrequestmethods
{
// 摘要:
// 表示可用于 file 请求的文件协议方法的类型。无法继承此类。
public static class file
{
// 摘要:
// 表示用来从指定的位置检索文件的 file get 协议方法。
public const string downloadfile = "get";
//
// 摘要:
// 表示用来将文件复制到指定位置的 file put 协议方法。
public const string uploadfile = "put";
}
// 摘要:
// 表示可与 ftp 请求一起使用的 ftp 协议方法的类型。无法继承此类。
public static class ftp
{
// 摘要:
// 表示要用于将文件追加到 ftp 服务器上的现有文件的 ftp appe 协议方法。
public const string appendfile = "appe";
//
// 摘要:
// 表示要用于删除 ftp 服务器上的文件的 ftp dele 协议方法。
public const string deletefile = "dele";
//
// 摘要:
// 表示要用于从 ftp 服务器下载文件的 ftp retr 协议方法。
public const string downloadfile = "retr";
//
// 摘要:
// 表示要用于从 ftp 服务器上的文件检索日期时间戳的 ftp mdtm 协议方法。
public const string getdatetimestamp = "mdtm";
//
// 摘要:
// 表示要用于检索 ftp 服务器上的文件大小的 ftp size 协议方法。
public const string getfilesize = "size";
//
// 摘要:
// 表示获取 ftp 服务器上的文件的简短列表的 ftp nlist 协议方法。
public const string listdirectory = "nlst";
//
// 摘要:
// 表示获取 ftp 服务器上的文件的详细列表的 ftp list 协议方法。
public const string listdirectorydetails = "list";
//
// 摘要:
// 表示在 ftp 服务器上创建目录的 ftp mkd 协议方法。
public const string makedirectory = "mkd";
//
// 摘要:
// 表示打印当前工作目录的名称的 ftp pwd 协议方法。
public const string printworkingdirectory = "pwd";
//
// 摘要:
// 表示移除目录的 ftp rmd 协议方法。
public const string removedirectory = "rmd";
//
// 摘要:
// 表示重命名目录的 ftp rename 协议方法。
public const string rename = "rename";
//
// 摘要:
// 表示将文件上载到 ftp 服务器的 ftp stor 协议方法。
public const string uploadfile = "stor";
//
// 摘要:
// 表示将具有唯一名称的文件上载到 ftp 服务器的 ftp stou 协议方法。
public const string uploadfilewithuniquename = "stou";
}
// 摘要:
// 表示可与 http 请求一起使用的 http 协议方法的类型。
public static class http
{
// 摘要:
// 表示与代理一起使用的 http connect 协议方法,该代理可以动态切换到隧道,如 ssl 隧道的情况。
public const string connect = "connect";
//
// 摘要:
// 表示一个 http get 协议方法。
public const string get = "get";
//
// 摘要:
// 表示一个 http head 协议方法。除了服务器在响应中只返回消息头不返回消息体以外,head 方法和 get 是一样的。
public const string head = "head";
//
// 摘要:
// 表示一个 http mkcol 请求,该请求在请求 uri(统一资源标识符)指定的位置新建集合,如页的集合。
public const string mkcol = "mkcol";
//
// 摘要:
// 表示一个 http post 协议方法,该方法用于将新实体作为补充发送到某个 uri。
public const string post = "post";
//
// 摘要:
// 表示一个 http put 协议方法,该方法用于替换 uri 标识的实体。
public const string put = "put";
}
}
}
|
以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持服务器之家。








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。