1. gzyueqian
      18529173453
      首頁 > 新聞中心 > > 正文

      用Java實現FTP服務器

      更新時間: 2007-06-25 13:06:48來源: 粵嵌教育瀏覽量:667

        FTP(File Transfer Protocol 文件傳輸協議)是Internet 上用來傳送文件的協議。在Internet上通過FTP 服務器可以進行文件的上傳(Upload)或下載(Download)。FTP是實時聯機服務,在使用它之前必須是具有該服務的一個用戶(用戶名和口令),工作時客戶端必須先登錄到作為服務器一方的計算機上,用戶登錄后可以進行文件搜索和文件傳送等有關操作,如改變當前工作目錄、列文件目錄、設置傳輸參數及傳送文件等。使用FTP可以傳送所有類型的文件,如文本文件、二進制可執行文件、圖象文件、聲音文件和數據壓縮文件等。

        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;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);
        }
       
        使用該命令時,客戶端必須發送客戶端用于接收數據的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;idirStructure.length;i++){
        if( dirStructure[i].indexOf(".") == -1) {

      免費預約試聽課

      亚洲另类欧美综合久久图片区_亚洲中文字幕日产无码2020_欧美日本一区二区三区桃色视频_亚洲AⅤ天堂一区二区三区

      
      

      1. 在线看片免费人成视频福利 | 中国浓毛少妇毛茸茸 | 亚洲综合色区在线观看 | 久久久免费精品视频 | 最新亚洲中文字幕无线 | 亚洲乱码尤物193yw |