FTP(File Transfer Protocol 文g传输协议)是Internet 上用来传送文件的协议。在Internet上通过FTP 服务器可以进行文件的上传(Upload)或下?Download)。FTP是实时联机服务,在用它之前必须是具有该服务的一个用?用户名和口o)Q工作时客户端必ddC为服务器一方的计算ZQ用L录后可以q行文g搜烦和文件传送等有关操作Q如改变当前工作目录、列文g目录、设|传输参数及传送文件等。用FTP可以传送所有类型的文gQ如文本文g、二q制可执行文件、图象文件、声x件和数据压羃文g{?
FTP 命o
FTP 的主要操作都是基于各U命令基之上的。常用的命o有:
?讄传输模式Q它包括ASC?文本) 和BINARY 二进制模?
?目录操作Q改变或昄q程计算机的当前目录(cd、dir/ls 命o);
?q接操作Qopen命o用于建立同远E计机的连接;close命o用于关闭q接;
?发送操作,put命o用于传送文件到q程计算机;mput 命o用于传送多个文件到q程计算?
?获取操作Qget命o用于接收一个文Ӟmget命o用于接收多个文g?
~程思\
ҎFTP 的工作原理,在主函数中徏立一个服务器套接字端口,{待客户端请求,一旦客Lh被接受,服务器程序就建立一个服务器分线E,处理客户端的命o。如果客L需要和服务器端q行文g的传输,则徏立一个新的套接字q接来完成文件的操作?
~程技巧说?
1.d数设?
在主函数中,完成服务器端口的侦听和服务线E的创徏。我们利用一个静态字W串变量initDir 来保存服务器U程q行时所在的工作目录。服务器的初始工作目录是q序运行时用户输入的,~省为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!");
//监听21L?
ServerSocket s = new ServerSocket(21);
for(;;){
//接受客户端请?
Socket incoming = s.accept();
//创徏服务U程
new ftpServer(incoming,i).start();
i++;
}
}catch(Exception e){}
}
2. U程cȝ设计
U程cȝ主要设计都是在run()Ҏ中实现。用run()Ҏ得到客户端的套接字信息,Ҏ套接字得到输入流和输出流Q向客户端发送欢q信息?
3. FTP 命o的处?
(1) 讉K控制命o
?user name(user) ?password (pass) 命o处理代码如下Q?
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.");
User 命o?Password 命o分别用来提交客户端用戯入的用户名和口o?
?CWD (CHANGE WORKING DIRECTORY) 命o处理代码如下Q?
if(str.startsWith("CWD")){
String str1 = str.substring(3);
dir = dir+"/"+str1.trim();
out.println("250 CWD command succesful");
}
该命令改变工作目录到用户指定的目录?
?CDUP (CHANGE TO PARENT DIRECTORY) 命o处理代码如下Q?
if(str.startsWith("CDUP")){
int n = dir.lastIndexOf("/");
dir = dir.substring(0,n);
out.println("250 CWD command succesful");
}
该命令改变当前目录ؓ上一层目录?
?QUIT命o处理代码如下Q?
if(str.startsWith("QUIT")) {
out.println("GOOD BYE");
done = true;
}
该命令退出及关闭与服务器的连接,输出GOOD BYE?
(2) 传输参数命o
?Port命o处理代码如下Q?
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;l<j;l++){
str1 = 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);
}
使用该命令时Q客L必须发送客L用于接收数据?2位IP 地址?6?的TCP 端口受这些信息以8位ؓ一l,使用十进制传输,中间用逗号隔开?
?TYPE命o处理代码如下Q?
if(str.startsWith("TYPE")){
out.println("200 type set");
}
TYPE 命o用来完成cd讄?
(3) FTP 服务命o
?RETR (RETEIEVE) ?STORE (STORE)命o处理的代?
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){}
}
文g传输命o包括从服务器中获得文件RETR和向服务器中发送文件STORQ这两个命o的处理非常类伹{处理RETR命oӞ首先得到用户要获得的文g的名UͼҎ名称创徏一个文件输入流Q然后和客户端徏立时套接字q接Qƈ得到一个输出流。随后,文件输入流中的数据dq借助于套接字输出发送到客户端,传输完毕以后Q关闭流和时套接字?
STOR 命o的处理也是同Lq程Q只是方向正好相反?
?DELE (DELETE)命o处理代码如下Q?
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 命o用于删除服务器上的指定文件?
?LIST命o处理代码如下Q?
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;i<dirStructure.length;i++){
if( dirStructure[i].indexOf(".") == -1) {
strType = "d ";}
else
{strType = "- ";}
out2.println(strType+dirStructure[i]);
}
tempSocket.close();
out.println("226 transfer complete");
}
catch(IOException e){}
LIST 命o用于向客Lq回服务器中工作目录下的目录l构Q包括文件和目录的列表。处理这个命令时Q先创徏一个时的套接字向客户端发送目录信息。这个套接字的目的端口号~省?Q然后ؓ当前工作目录创徏File 对象Q利用该对象的list()Ҏ得到一个包含该目录下所有文件和子目录名U的字符串数l,然后Ҏ名称中是否含有文件名中特有的“.”来区别目录和文g。最后,得到的名称数组通过临时套接字发送到客户端?