论坛: 编程破解 标题: FTP基本控制的方法(class(java)) 复制本贴地址    
作者: kenter1643 [kenter1643]    论坛用户   登录
FTP基本控制的方法(几天终于搞好了类和方法)
package ftp;

import java.net.*;
import java.io.*;

public class FTP {
  Socket ctrlSocket;
  public PrintWriter ctrlOutput;
  public BufferedReader ctrlInput;
  final int CTRLPORT = 21; /*用的端口*/
  public void openConection(String host) throws IOException,
      UnknownHostException {
    ctrlSocket = new Socket(host, CTRLPORT);
    ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream());
    ctrlInput = new BufferedReader(new InputStreamReader(ctrlSocket.
        getInputStream()));

  }

  public void closeConnection() throws IOException{
    ctrlSocket.close();
  }
  public void showMenu()
  {
    System.out.println(">Command?");
    System.out.println("2 ls");
    System.out.println("3 cd");
    System.out.println("4 get");
    System.out.println("5 put");
    System.out.println("6 ascii");
    System.out.println("7 binary");
    System.out.println("  9 quit");
  }
  public String getCommand()
  {
    String buf="";
        BufferedReader lineread=new BufferedReader(new InputStreamReader(System.in));
        while(buf.length() !=1){
          try{
            buf=lineread.readLine() ;
          }catch(Exception e){
          e.printStackTrace() ;
          System.exit(1) ;
          }
        }
        return (buf);
  }
  public void doLogin()
  {
    String loginName="";
    String password="";
    BufferedReader lineread=new BufferedReader(new InputStreamReader(System.in));
    try{
      System.out.println("请输入用户名") ;
      loginName=lineread.readLine() ;
      ctrlOutput.println("USER"+loginName) ;
      ctrlOutput.flush() ;
      System.out.println("请输入口令") ;
      password=lineread.readLine() ;
      ctrlOutput.println("PASS"+password) ;
      ctrlOutput.flush() ;
    }catch(Exception e)
    {e.printStackTrace() ;
      System.exit(1) ;
    }
  }
  public void doQuit()
  {
    try{
      ctrlOutput.println("QUIT") ;
      ctrlOutput.flush() ;
    }catch(Exception e)
    {e.printStackTrace() ;
      System.exit(1) ;
    }
  }
  public void doCd()
  {
    String dirName="";
    BufferedReader lineread=new BufferedReader(new InputStreamReader(System.in));
    try{
      System.out.println("请输入目录名:") ;
      dirName=lineread.readLine() ;
      ctrlOutput.print("CWD"+dirName) ;
      ctrlOutput.flush() ;
    }catch(Exception e){
      e.printStackTrace() ;
      System.exit(1) ;
    }
}
  public void doLs()
  {
    try{
      int n;
      byte[] buff=new byte[1024];
      Socket dataSocket=dataConnection("LIST");
      BufferedInputStream dataInput=new BufferedInputStream(dataSocket.getInputStream() );
      while((n=dataInput.read(buff))>0) {
        System.out.write(buff,0,n);
      }
      dataSocket.close() ;
    }catch(Exception e)
    {e.printStackTrace() ;
    System.exit(1) ;
    }
  }
  public Socket dataConnection(String ctrlcmd)
  {
    String cmd="PORT";
    int i;
    Socket dataSocket=null;
    try{
      byte[] address=InetAddress.getLocalHost() .getAddress() ;
      ServerSocket serverDataSocket=new ServerSocket(0,1);
      for(i=0;i<4;++i)
        cmd=cmd+(((serverDataSocket.getLocalPort() )/256)&0Xff)+"'"
            +(serverDataSocket.getLocalPort() &0Xff);
        ctrlOutput.println(cmd);
        ctrlOutput.flush() ;
        dataSocket=serverDataSocket.accept() ;
        serverDataSocket.close() ;
    }catch(Exception e) {
        e.printStackTrace() ;
        System.exit(1) ;
    }
    return dataSocket;
  }
  public void doAscii()
  {
    try{
      ctrlOutput.println("TYPE A") ;
      ctrlOutput.flush() ;
    }catch(Exception e){
      e.printStackTrace() ;
      System.exit(1) ;
    }
  }
  public void doBinary()
  {
    try{
      ctrlOutput .println("TYPE I") ;
      ctrlOutput.flush() ;
    }catch(Exception e){
      e.printStackTrace() ;
      System.exit(1) ;
    }
}
  public void doGet()
  {
    String fileName="";
    BufferedReader lineread=new BufferedReader (new InputStreamReader(System.in));
    try{
      int n;
      byte[] buff=new byte[1024];
      System.out.println("请输入文件名") ;
      fileName=lineread.readLine() ;
      FileOutputStream outfile=new FileOutputStream(fileName);
      Socket dataSocket=dataConnection("RETR"+fileName);
      BufferedInputStream dataInput=new BufferedInputStream(dataSocket.getInputStream() );
      while((n=dataInput.read(buff) )>0){
        outfile.write(buff,0,n) ;
      }
      dataSocket.close() ;
      outfile.close() ;
    }catch(Exception e){
      e.printStackTrace() ;
      System.exit(1) ;
      }
    }
    public void doPut()
    {
      String fileName="";
      BufferedReader lineread=new BufferedReader(new InputStreamReader(System.in));
      try{
        int n;
        byte[] buff=new byte[1024];
        FileInputStream sendfile=null;
        System.out.println("请输入文件名") ;
        fileName=lineread.readLine() ;
        try{
          sendfile=new FileInputStream(fileName);
        }catch(Exception e){
          System.out.println("文件不存在") ;
          return;
        }
        Socket dataSocket=dataConnection("STOR"+fileName);
        OutputStream outstr=dataSocket.getOutputStream() ;
        while((n=sendfile.read(buff))>0){
          outstr.write(buff,0,n) ;
        }
        dataSocket.close() ;
        sendfile.close() ;
        }catch(Exception e){
          e.printStackTrace() ;
          System.exit(1) ;
      }
  }
  public boolean execCommand(String command)
  {
    boolean cont=true;
    switch(Integer.parseInt(command )){
    case 2:doLs();break;
    case 3:doCd();break;
    case 4:doGet();break;
    case 5:doPut();break;
    case 6:doAscii();break;
    case 7:doBinary();break;
    case 9:doQuit();cont=false;break;
    default:System.out .println("请选择一个序号") ;
    }
    return(cont);
  }
  public void main_proc() throws IOException
  {
    boolean cont=true;
    try{
      doLogin();
      while(cont){
        showMenu();
        cont=execCommand(getCommand());
      }
    }catch(Exception e){
      System.err.print(e) ;
      System.exit(1) ;
      }
    }
    /*启动接收控制流的线程*/
    public void getMsgs(){
      try{
        CtrlListen listener=new CtrlListen(ctrlInput);
        Thread listenerthread=new Thread(listener);
        listenerthread.start() ;
      }catch(Exception e){
        e.printStackTrace() ;
        System.exit(1);
      }
  }
  class CtrlListen implements Runnable{
    BufferedReader ctrlInput=null;
    public CtrlListen(BufferedReader in){
      ctrlInput=in;
    }
    public void run(){
        while(true){
          try{
            System.out.println(ctrlInput.readLine() ) ;
          }catch(Exception e){
            System.exit(1) ;
          }
        }
    }
  }
  public static void main(String[] arg){
    try{
      FTP f=null;
      if(arg.length <1){
        System.out.print("usage:java FTP<host name>") ;
        return;
      }
      f=new FTP();
      f.openConection(arg[0]) ;
      f.getMsgs();
      f.main_proc();
      f.closeConnection() ;
      System.exit(1) ;
    }catch(Exception e){
      e.printStackTrace() ;
      System.exit(1) ;
    }
  }
}


地主 发表时间: 04-02-10 23:15

论坛: 编程破解

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号