FTP协议的命令的使用(转)

 
FTP 协议命令

如果想自己编写一个FTP客户端程序,首先得搞清楚FTP协议中的这些命令和用法。

FTP COMMANDS

         The following are the FTP commands:

            USER <SP> <username> <CRLF>       登录的用户名
            PASS <SP> <password> <CRLF>       密码
            ACCT <SP> <account-information> <CRLF>
            CWD  <SP> <pathname> <CRLF>       改变当前目录
            CDUP <CRLF>                       返回上级目录
            SMNT <SP> <pathname> <CRLF>
            QUIT <CRLF>                       退出
            REIN <CRLF>                       重新登录
            PORT <SP> <host-port> <CRLF>
            PASV <CRLF>                       获取数据传输端口
            TYPE <SP> <type-code> <CRLF>      设置数据交换的类型{A(SCII),E(BCDIC),I(mage),L(ocal byte size)}
            STRU <SP> <structure-code> <CRLF>
            MODE <SP> <mode-code> <CRLF>
            RETR <SP> <pathname> <CRLF>
            STOR <SP> <pathname> <CRLF>       用覆盖方式上传一个文件到服务器
            STOU <CRLF>
            APPE <SP> <pathname> <CRLF>       用追加方式上传一个文件到服务器
            ALLO <SP> <decimal-integer>
                [<SP> R <SP> <decimal-integer>] <CRLF>
            REST <SP> <marker> <CRLF>         指定重新下在的字节数
            RNFR <SP> <pathname> <CRLF>       指定需要改名的原文件
            RNTO <SP> <pathname> <CRLF>       指定需要改名的新文件名
            ABOR <CRLF>                       取消前一指令
            DELE <SP> <pathname> <CRLF>       在服务器上删除指定文件
            RMD  <SP> <pathname> <CRLF>       在服务器上删除指定目录
            MKD  <SP> <pathname> <CRLF>       在服务器上创建目录      
            PWD  <CRLF>                       显示当前目录
            LIST [<SP> <pathname>] <CRLF>     列出当前目录下所有文件和子目录
            NLST [<SP> <pathname>] <CRLF>     列出当前目录下所有文件和子目录的名字
            SITE <SP> <string> <CRLF>
            SYST <CRLF>
            STAT [<SP> <pathname>] <CRLF>
            HELP [<SP> <string>] <CRLF>       获取帮助
            NOOP <CRLF>

FTP Security commands and arguments

      AUTH <SP> <mechanism-name> <CRLF>
      ADAT <SP> <base64data> <CRLF>
      PROT <SP> <prot-code> <CRLF>
      PBSZ <SP> <decimal-integer> <CRLF>
      MIC <SP> <base64data> <CRLF>
      CONF <SP> <base64data> <CRLF>
      ENC <SP> <base64data> <CRLF>

      <mechanism-name> ::= <string>
      <base64data> ::= <string>
              ; must be formatted as described in section 9
      <prot-code> ::= C | S | E | P
      <decimal-integer> ::= any decimal integer from 1 to (2^32)-1

 

上面是一些ftp协议支持的命令,下面介绍一些使用实例

FTP 协议命令实战

废话就不多说了,下面是用TELNET连接Serv-U测试成功的例子

下载文件
---------------------------------------------------------------------------
TELNET 127.0.0.1 21
220 Serv-U FTP Server v4.0 for WinSock ready...

USER ADMIN
331 User name okay, need password.

PASS ****
230 User logged in, proceed.

PASV
227 Entering Passive Mode <127,0,0,1,4,191>

REST 0
350 Restarting at 0. Send STORE or RETRIEVE.

RETR test.rar
150 Opening ASCII mode data connection for test.rar <94370 bytes>.
226 Transfer complete.

 

覆盖方式上传文件
---------------------------------------------------------------------------
TELNET 127.0.0.1 21
220 Serv-U FTP Server v4.0 for WinSock ready...

USER ADMIN
331 User name okay, need password.

PASS ****
230 User logged in, proceed.

STOR test.dat
150 Opening ASCII mode data connection for test.dat

PASV
227 Entering Passive Mode <127,0,0,1,4,191>

...创建另一条连接到指定端口然后向其发送数据...

 

追加方式上传文件
---------------------------------------------------------------------------
TELNET 127.0.0.1 21
220 Serv-U FTP Server v4.0 for WinSock ready...

USER ADMIN
331 User name okay, need password.

PASS ****
230 User logged in, proceed.

APPE test.dat
150 Opening ASCII mode data connection for test.dat

PASV
227 Entering Passive Mode <127,0,0,1,4,191>

...创建另一条连接到指定端口然后向其发送数据...

 

重命名文件和目录
---------------------------------------------------------------------------
TELNET 127.0.0.1 21
220 Serv-U FTP Server v4.0 for WinSock ready...

USER ADMIN
331 User name okay, need password.

PASS ****
230 User logged in, proceed.

RNFR sss.rar
350 File or directory exists; ready for destination name

RNTO stock.rar
250 RNTO command successfull.

 

删除文件
---------------------------------------------------------------------------
TELNET 127.0.0.1 21
220 Serv-U FTP Server v4.0 for WinSock ready...

USER ADMIN
331 User name okay, need password.

PASS ****
230 User logged in, proceed.

DELE test.dat
250 DELE command successfull.

 

建立和删除目录
---------------------------------------------------------------------------
TELNET 127.0.0.1 21
220 Serv-U FTP Server v4.0 for WinSock ready...

USER ADMIN
331 User name okay, need password.

PASS ****
230 User logged in, proceed.

MKD myDir
257 "/myDir" directory created.

RMD myDir
250 RMD command successfull.

 

退出连接
---------------------------------------------------------------------------
TELNET 127.0.0.1 21
220 Serv-U FTP Server v4.0 for WinSock ready...

USER ADMIN
331 User name okay, need password.

PASS ****
230 User logged in, proceed.

QUIT
221 Goodbye!

上一篇:Maven企业服务器搭建


下一篇:【Groovy】集合遍历 ( 使用 for 循环遍历集合 | 使用集合的 each 方法遍历集合 | 集合的 each 方法返回值分析 )