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 指令的處理
(1) 通路控制指令
◆ 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.");
User 指令和 Password 指令分别用來送出用戶端使用者輸入的使用者名和密碼。
◆ CWD (CHANGE WORKING DIRECTORY) 指令處理代碼如下:
if(str.startsWith("CWD")){
String str1 = str.substring(3);
dir = dir+"/"+str1.trim();
out.println("250 CWD command succesful");
}
該指令改變工作目錄到使用者指定的目錄。
◆ CDUP (CHANGE TO PARENT DIRECTORY) 指令處理代碼如下:
if(str.startsWith("CDUP")){
int n = dir.lastIndexOf("/");
dir = dir.substring(0,n);
out.println("250 CWD command succesful");
}
該指令改變目前目錄為上一層目錄。
◆ QUIT指令處理代碼如下:
if(str.startsWith("QUIT")) {
out.println("GOOD BYE");
done = true;
}
該指令退出及關閉與伺服器的連接配接,輸出GOOD BYE。
(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 = 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){}
}
檔案傳輸指令包括從伺服器中獲得檔案RETR和向伺服器中發送檔案STOR,這兩個指令的處理非常類似。處理RETR指令時,首先得到使用者要獲得的檔案 的名稱,根據名稱建立一個檔案輸入流,然後和用戶端建立臨時套接字連接配接,并得到一個輸出流。随後,将檔案輸入流中的資料讀出并借助于套接字輸出流發送到客 戶端,傳輸完畢以後,關閉流和臨時套接字。
STOR 指令的處理也是同樣的過程,隻是方向正好相反。
◆ DELE (DELETE)指令處理代碼如下:
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 指令用于删除伺服器上的指定檔案。
◆ LIST指令處理代碼如下:
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){}
LIST 指令用于向用戶端傳回伺服器中工作目錄下的目錄結構,包括檔案和目錄的清單。處理這個指令時,先建立一個臨時的套接字向用戶端發送目錄資訊。這個套接字的 目的端口号預設為1,然後為目前工作目錄建立File 對象,利用該對象的list()方法得到一個包含該目錄下所有檔案和子目錄名稱的字元串數組,然後根據名稱中是否含有檔案名中特有的“.”來差別目錄和文 件。最後,将得到的名稱數組通過臨時套接字發送到用戶端。