欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
(2) 传输参数命令
◆ Port命令处理代码如下:
if(str.startsWith(“PORT”)) {out.println(“200 PORT command successful”);int i = str.length() – 1;int j = str.lastIndexOf(“,”);int k = str.lastIndexOf(“,”,j-1);String str1,str2;str1=””;str2=””;for(int l=k+1;lstr1 = str2 + str.charAt(l);}for(int l=j+1;l<=i;l++){str2 = str2 + str.charAt(l);}tempPort = Integer.parseInt(str1) * 16 *16 +Integer.parseInt(str2);}
使用该命令时,客户端必须发送客户端用于接收数据的32位IP 地址和16位 的TCP 端口号。这些信息以8位为一组,使用十进制传输,中间用逗号隔开。
◆ TYPE命令处理代码如下:
if(str.startsWith(“TYPE”)){out.println(“200 type set”);}
TYPE 命令用来完成类型设置。
(3) FTP 服务命令
◆ RETR (RETEIEVE) 和 STORE (STORE)命令处理的代码:
if(str.startsWith(“RETR”)){out.println(“150 Binary data connection”);str = str.substring(4);str = str.trim();RandomAccessFile outFile = newRandomAccessFile(dir+”/”+str,”r”);Socket tempSocket = new Socket(host,tempPort);OutputStream outSocket= tempSocket.getOutputStream();byte byteBuffer[]= new byte[1024];int amount;try{while((amount = outFile.read(byteBuffer)) != -1){outSocket.write(byteBuffer, 0, amount);}outSocket.close();out.println(“226 transfer complete”);outFile.close();tempSocket.close();}catch(IOException e){}}if(str.startsWith(“STOR”)){out.println(“150 Binary data connection”);str = str.substring(4);str = str.trim();RandomAccessFile inFile = newRandomAccessFile(dir+”/”+str,”rw”);Socket tempSocket = new Socket(host,tempPort);InputStream inSocket= tempSocket.getInputStream();byte byteBuffer[] = new byte[1024];int amount;try{while((amount =inSocket.read(byteBuffer) )!= -1){inFile.write(byteBuffer, 0, amount);}inSocket.close();out.println(“226 transfer complete”);inFile.close();tempSocket.close();}catch(IOException e){}}