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


 
/** 
 * Created by snake. 
 * User: snake 
 * Date: 2004-3-11 
 * Time: 21:24:52 
 */ 
 
package FTPclient; 
import java.net.*; 
import java.io.*; 
 
public class FTPclient { 
      FileOutputStream outfile; 
      String info,flag; 
      String list[]; 
 
      // getflag()and setflag() provide to the FTP.jsp as the interface 
      public String getflag(){ 
           return this.flag; 
      } 
      public void setflag(String flag){ 
           this.flag = flag; 
      } 
 
      public  static void main(String [] args) { 
 
          //Appoint the "pict1322.jpg" as the requested file and receive the file; 
           String filename = "pict1322.jpg"; 
           FTPclient fc = new FTPclient(); 
           fc.getlist(); 
           fc.client(filename); 
    } 
 
 
    public String client(String filename){ 
        //connected the FTPserver and receive the appointed the file; 
           try{ 
                 Socket sock =new Socket ("snake",9990); 
                 DataInputStream in = new DataInputStream(new BufferedInputStream(sock.getInputStream())); 
                 PrintStream out = new PrintStream(new BufferedOutputStream(sock.getOutputStream())); 
 
                 setflag(filename); 
                 out.println(getflag()); 
                 out.flush() ; 
 
                 int t,len,bt_len = 8192; 
                 byte pipe[] = new byte[bt_len]; 
 
                 //appointed the path so as to save the file; 
                 String file,path = "c:\\" ; 
                 file = path + filename; 
 
                 outfile =new FileOutputStream (file)  ; 
                 info = filename; 
                 //receive the bytestream accoding to the "header"("header" is the valid length) 
                 while ((t=in.read(pipe))!=-1 ){ 
                         len = (int)pipe[0] * 100 + (int)pipe[1]; 
                         if (len < 0) {continue;} 
                         outfile.write(pipe,2,len) ; 
                 } 
               outfile.close(); 
             } 
               catch(IOException e){System.out.println(e.toString());} 
               return "Transation End." ; 
      } 
     public String[] getlist(){ 
         // receive the catalog file information provided by  the FTPserver ; 
          try{ 
                 Socket sock =new Socket ("snake",9990); 
                 DataInputStream in = new DataInputStream(new BufferedInputStream(sock.getInputStream())); 
                 PrintStream out = new PrintStream(new BufferedOutputStream(sock.getOutputStream())); 
 
 
                 setflag("catalog"); 
                 out.println(getflag() + "\r"); 
                 out.flush() ; 
 
                 int list_length = in.read(); 
                 list = new String[list_length]; 
                 for (int i= 0;i < list_length;i++){ 
                             list[i] = in.readLine(); 
                       } 
                 in.close(); 
                 out.close(); 
                 sock.close(); 
           } 
                 catch(IOException e){System.err.println(e);} 
                 return list; 
      } 
 
      public String getinfo(){return this.info;} 
}