www.pudn.com > FTPserver.rar > FTPserver.java


 
/* 
 * Created by snake. 
 * User: Snake 
 * Date: 2004-3-11 
 * Time: 21:38:49 
 */ 
 
import java.net.*; 
import java.io.*; 
 
public class FTPserver { 
       private static final int port=9990; 
       private static final int maxQ=10; 
 
       // init filename so as to debug the program 
       String info,filename = "celeron.jpg"; 
 
       // Start FTPserver 
       public static void main(String args[]){ 
               new FTPserver().handleClients(); 
              } 
 
      //dispose the request from client accoding to the variable of "flag" 
     //if "flag = catalog" then transfer catalog of files in FTPserver end. 
    //if "flag = file" then transfer file appointed by client; 
      public String handleClients() { 
        try{ 
            ServerSocket servSock=new ServerSocket(port,maxQ) ; 
            info = "FTP Server has started......."; 
            String flag  = "file"; 
            while(true) { 
 
                Socket sock=servSock.accept(); 
                System.out.println("Connected."); 
                ConnFlag cf = new ConnFlag(); 
                flag = cf.getflag(sock); 
                System.out.println(flag); 
 
                if  (flag.equals("catalog")) { 
                    FileDialog fd = new FileDialog(); 
                    fd.SendCatalog(sock); 
                    flag = "file"; 
                    continue; 
                } 
                filename = flag; 
                new DownLoad(sock,filename).start(); 
            } 
 
        } catch(IOException e) {System.err.println(e);} 
        return info; 
    } 
 
 
} 
 class FileDialog{ 
     //This class transfers the file catalog of the FTPserver end to the client end; 
 
     public String[]  FileDialog(){ 
           String sdir = "G:\\temp\\FTP-1"; 
           File Fdir = new File(sdir); 
           String list[] = new String[Fdir.list().length]; 
           for(int i = 0;i < Fdir.list().length;i++){ 
                 list[i] = (Fdir.list())[i]; 
               } 
               return list; 
           } 
     public void SendCatalog(Socket sock){ 
         try{ 
               String list[]; 
               list = FileDialog(); 
 
               DataInputStream in = new DataInputStream(new BufferedInputStream(sock.getInputStream())); 
               BufferedInputStream is = new BufferedInputStream(sock.getInputStream()); 
               PrintStream out = new PrintStream(new BufferedOutputStream(sock.getOutputStream())); 
               BufferedOutputStream os = new BufferedOutputStream(sock.getOutputStream()); 
 
               out.write((byte)list.length); 
               out.flush(); 
 
               for(int i = 0;i < list.length;i++){ 
                    out.println(list[i] ); 
                    out.flush(); 
               } 
               in.close(); 
               out.close(); 
               sock.close(); 
               System.out.println("close."); 
            } 
              catch(IOException e){System.err.println(e);} 
 
          } 
 
} 
class DownLoad extends Thread{ 
    //This class transfers the file appointed by the client; 
        private Socket sock; 
        private FileInputStream infile; 
        private String file; 
 
 
       private byte[] getlength(int len){ 
           // This function calculates the length of bytestream in every reading; 
 
            byte length[] = new byte[2]; 
            int temp[] = new int[4]; 
            int num = 4,divide = 1,temp_len = len; 
 
            for(int i = 0;i < num - 1;i++){ 
                 divide *= 10; 
               } 
            for (int i = 0;i < num ;i++){ 
                 temp[i] = temp_len / divide; 
                 temp_len = temp_len - temp[i] * divide; 
                divide = divide / 10; 
            } 
            length[0] = (byte)(temp[0] * 10 + temp[1]); 
            length[1] = (byte)(temp[2] * 10 + temp[3]); 
 
            return length; 
      } 
     public DownLoad (Socket s,String filename) { 
             sock =s ; 
             file=filename; 
        } 
    public void run(){ 
            try { 
                   int bt_len = 8192; 
                   byte pipe[] = new byte[bt_len]; 
 
                   PrintStream out = new PrintStream(new BufferedOutputStream(sock.getOutputStream())); 
 
                   infile=new FileInputStream(file); 
                   int len ; 
 
                   byte[] length_buff = new byte[2]; 
                    System.out.println(file); 
 
               while ((len=infile.read(pipe,2,bt_len - 2))!=-1 ){ 
                       length_buff = getlength(len); 
                       System.out.print((int)length_buff[0]); 
                       System.out.println((int)length_buff[1]); 
 
                       //add "header" to the datagram; 
                       System.arraycopy(length_buff,0,pipe,0,2 ); 
                       out.write(pipe); 
                   } 
                  sock.close(); 
                  System.out.println("close."); 
        }  catch (IOException e) {System.err.println(e);} 
 
    } 
 
 
} 
class ConnFlag{ 
    //This class receives the flag requested of client; 
 
    String flag; 
    public String getflag(Socket sock){ 
       try{ 
              DataInputStream in = new DataInputStream(new BufferedInputStream(sock.getInputStream())); 
              BufferedInputStream is = new BufferedInputStream(sock.getInputStream()); 
              PrintStream out = new PrintStream(new BufferedOutputStream(sock.getOutputStream())); 
              BufferedOutputStream os = new BufferedOutputStream(sock.getOutputStream()); 
 
              return in.readLine(); 
       } 
       catch(IOException e){return e.toString();} 
   } 
 
}