FTP 命令
FTP 的主要操作都是基于各种命令基础之上的。常用的命令有:
◆ 设置传输模式,它包括ASCⅡ(文本) 和BINARY 二进制模式;
◆ 目录操作,改变或显示远程计算机的当前目录(cd、dir/ls 命令);
◆ 连接操作,open命令用于建立同远程计算机的连接;close命令用于关闭连接;
◆ 发送操作,put命令用于传送文件到远程计算机;mput 命令用于传送多个文件到远程计算机;
◆ 获取操作,get命令用于接收一个文件;mget命令用于接收多个文件。
编程思路
根据FTP 的工作原理,在主函数中建立一个服务器套接字端口,等待客户端请求,一旦客户端请求被接受,服务器程序就建立一个服务器分线程,处理客户端的命令。如果客户端需要和服务器端进行文件的传输,则建立一个新的套接字连接来完成文件的操作。
编程技巧说明
1.主函数设计
在主函数中,完成服务器端口的侦听和服务线程的创建。我们利用一个静态字符串变量initDir 来保存服务器线程运行时所在的工作目录。服务器的初始工作目录是由程序运行时用户输入的,缺省为C盘的根目录。
|
public class ftpServer extends Thread{ private Socket socketClient; private int counter; private static String initDir; public static void main(String[] args){ if(args.length != 0) { initDir = args[0]; }else{ initDir = "c:";} int i = 1; try{ System.out.println("ftp server started!"); //监听21号端口 ServerSocket s = new ServerSocket(21); for(;;){ //接受客户端请求 Socket incoming = s.accept(); //创建服务线程 new ftpServer(incoming,i).start(); i++; } }catch(Exception e){} |
2. 线程类的设计
线程类的主要设计都是在run()方法中实现。用run()方法得到客户端的套接字信息,根据套接字得到输入流和输出流,向客户端发送欢迎信息。
3. FTP 命令的处理
|
◆ user name(user) 和 password (pass) 命令处理代码如下: if(str.startsWith("USER")){ user = str.substring(4); user = user.trim(); out.println("331 Password");} if(str.startsWith("PASS")) out.println("230 User "+user+" logged in."); |
|
if(str.startsWith("CWD")){ String str1 = str.substring(3); dir = dir+"/"+str1.trim(); out.println("250 CWD command succesful"); } |
|
if(str.startsWith("CDUP")){ int n = dir.lastIndexOf("/"); dir = dir.substring(0,n); out.println("250 CWD command succesful"); } |
|
if(str.startsWith("QUIT")) { out.println("GOOD BYE"); done = true; } |
|
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); |
|
if(str.startsWith("TYPE")){ out.println("200 type set"); } |
|
if(str.startsWith("RETR")){ out.println("150 Binary data connection"); str = str.substring(4); str = str.trim(); RandomAccessFile outFile = new RandomAccessFile(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 = new RandomAccessFile(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){} } |
|
if(str.startsWith("DELE")){ str = str.substring(4); str = str.trim(); File file = new File(dir,str); boolean del = file.delete(); out.println("250 delete command successful"); } |
DELE 命令用于删除服务器上的指定文件。
|
if(str.startsWith("LIST")) { try{ out.println("150 ASCII data"); Socket tempSocket = new Socket(host,tempPort); PrintWriter out2= new PrintWriter(tempSocket.getOutputStream(),true); File file = new File(dir); String[] dirStructure = new String[10]; dirStructure= file.list(); String strType=""; for(int i=0;iif( dirStructure[i].indexOf(".") == -1) { strType = "d ";} else {strType = "- ";} out2.println(strType+dirStructure[i]); } tempSocket.close(); out.println("226 transfer complete"); } catch(IOException e){} |