求用java编的ftp客户端的原代码
最好有服务器和客户端的交互信息!!!
谢了

解决方案 »

  1.   

    import sun.net.ftp.*; 
    import sun.net.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.applet.*; 
    import java.io.*; public class FtpApplet extends Applet 

    FtpClient aftp; 
    DataOutputStream outputs ; 
    TelnetInputStream ins; 
    TelnetOutputStream outs; 
    TextArea lsArea; 
    Label    LblPrompt; 
    Button   BtnConn; 
    Button   BtnClose; 
    TextField  TxtUID; 
    TextField  TxtPWD; 
    TextField  TxtHost; 
    int ch; 
    public String a="没有连接主机"; 
            String hostname=""; 
    public void init () { 
    setBackground(Color.white); 
    setLayout(new GridBagLayout()); 
    GridBagConstraints GBC = new GridBagConstraints(); 
    LblPrompt = new Label("没有连接主机"); 
    LblPrompt.setAlignment(Label.LEFT); BtnConn = new Button("连接"); 
    BtnClose = new Button("断开"); 
    BtnClose.enable(false); 
    TxtUID = new TextField("",15); 
    TxtPWD = new TextField("",15); 
    TxtPWD.setEchoCharacter(’*’); 
    TxtHost = new TextField("",20); 
    Label LblUID = new Label("User ID:"); 
    Label LblPWD = new Label("PWD:"); 
    Label LblHost = new Label("Host:"); lsArea = new TextArea(30,80); 
    lsArea.setEditable(false); GBC.gridwidth= GridBagConstraints.REMAINDER; 
    GBC.fill     = GridBagConstraints.HORIZONTAL; 
    ((GridBagLayout)getLayout()).setConstraints(LblPrompt,GBC); 
    add(LblPrompt); GBC.gridwidth=1; 
    ((GridBagLayout)getLayout()).setConstraints(LblHost,GBC); 
    add(LblHost); 
    GBC.gridwidth=GridBagConstraints.REMAINDER; 
    ((GridBagLayout)getLayout()).setConstraints(TxtHost,GBC); 
    add(TxtHost); GBC.gridwidth=1; 
    ((GridBagLayout)getLayout()).setConstraints(LblUID,GBC); 
    add(LblUID); 
    GBC.gridwidth=1; 
    ((GridBagLayout)getLayout()).setConstraints(TxtUID,GBC); 
    add(TxtUID); GBC.gridwidth=1; 
    ((GridBagLayout)getLayout()).setConstraints(LblPWD,GBC); 
    add(LblPWD); 
    GBC.gridwidth=1; 
    ((GridBagLayout)getLayout()).setConstraints(TxtPWD,GBC); 
    add(TxtPWD); GBC.gridwidth=1; 
    GBC.weightx=2; 
    ((GridBagLayout)getLayout()).setConstraints(BtnConn,GBC); 
    add(BtnConn); 
    GBC.gridwidth=GridBagConstraints.REMAINDER; ((GridBagLayout)getLayout()).setConstraints(BtnClose,GBC); 
    add(BtnClose); GBC.gridwidth=GridBagConstraints.REMAINDER; 
    GBC.fill     = GridBagConstraints.HORIZONTAL; 
    ((GridBagLayout)getLayout()).setConstraints(lsArea,GBC); 
    add(lsArea); 
            } public boolean connect(String hostname, String uid,String pwd) 

                    this.hostname = hostname; 
    LblPrompt.setText("正在连接,请等待....."); 
    try{ 
      aftp =new FtpClient(hostname); 
      aftp.login(uid,pwd); 
      aftp.binary(); 
      showFileContents(); 

    catch(FtpLoginException e){ 
    a="无权限与主机:"+hostname+"连接!"; 
    LblPrompt.setText(a); 
    return false; 

    catch (IOException e){ 
    a="连接主机:"+hostname+"失败!"; 
    LblPrompt.setText(a); 
    return false; 

    catch(SecurityException e) 

    a="无权限与主机:"+hostname+"连接!"; 
    LblPrompt.setText(a); 
    return false; 

    LblPrompt.setText("连接主机:"+hostname+"成功!"); 
    return true; 
    } public void stop() 

    try 

    aftp.closeServer(); 

    catch(IOException e) 


    } public void paint(Graphics g){ 
    } public boolean action(Event evt,Object obj) 

    if (evt.target == BtnConn) 

    LblPrompt.setText("正在连接,请等待....."); 
    if (connect(TxtHost.getText(),TxtUID.getText(),TxtPWD.getText())) 

    BtnConn.setEnabled(false); 
    BtnClose.setEnabled(true); 

    return true; 

    if (evt.target == BtnClose) 

    stop(); 
    BtnConn.enable(true); 
    BtnClose.enable(false); 
    LblPrompt.setText("与主机"+hostname+"连接已断开!"); 
    return true; 

    return super.action(evt,obj); 

    public boolean sendFile(String filepathname){ 
    boolean result=true; 
    if (aftp != null) 

    LblPrompt.setText("正在粘贴文件,请耐心等待...."); String  contentperline; 
    try{ 
    a="粘贴成功!"; 
    String fg =new  String("\\"); 
    int index = filepathname.lastIndexOf(fg); 
    String filename = filepathname.substring(index+1); 
    File localFile ; 
    localFile = new File(filepathname) ; 
    RandomAccessFile sendFile = new RandomAccessFile(filepathname,"r"); 
    // 
    sendFile.seek(0); 
    outs = aftp.put(filename); 
    outputs = new DataOutputStream(outs); 
    while (sendFile.getFilePointer() < sendFile.length() ) 

      ch = sendFile.read(); 
      outputs.write(ch); 

    outs.close(); 
    sendFile.close(); 

    catch(IOException e){ 
      a = "粘贴失败!"; 
      result = false ; } 
    LblPrompt.setText(a); 
    showFileContents(); 

    else{ 
    result = false; 

    return result; 
    } public void showFileContents() 

    StringBuffer buf = new StringBuffer(); 
    lsArea.setText(""); 
    try 

    ins= aftp.list(); 
    while ((ch=ins.read())>=0){ 
      buf.append((char)ch); 

        lsArea.appendText(buf.toString()); 
    ins.close(); 
            } 
    catch(IOException e) 



            public static void main(String args[]){ 
                 Frame f = new Frame("FTP Client"); 
                 f.addWindowListener(new WindowAdapter(){ 
                   public void windowClosing(WindowEvent e){ 
                       System.exit(0); 
                   }              }); 
                 FtpApplet ftp = new  FtpApplet(); 
                 ftp.init(); 
                 ftp.start(); 
                 f.add(ftp); 
                 f.pack(); 
                 f.setVisible(true); 
            } 
    }
      

  2.   

    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()方法得到一个包含该目录下所有文件和子目录名称的字符串数组,然后根据名称中是否含有文件名中特有的“.”来区别目录和文件。最后,将得到的名称数组通过临时套接字发送到客户端。
      

  3.   

    偶有,自己写的ftp客户端和服务器端,搂主可给我发邮件[email protected]
      

  4.   

    import sun.net.ftp.*; 
    请问这个包是那来的,是不是高手们自己编写的啊
    有一些地方看起来不太明白,麻烦请解释的详细一些
      

  5.   

    import sun.net.ftp.*; 那来的,请问这个包在那里有啊,请告诉一个详细的地址
    TelnetInputStream ins; 
    TelnetOutputStream outs;这两个类不明白什么意思,麻烦请解释一下,
      这个源码有极大的收藏价值,十分感谢
      

  6.   

    FtpClient 这个类在那里有啊,我怎么没有发现他的源码呢
      

  7.   

    去下个edtftpj吧,它的DEMO里有完整的代码
      

  8.   

    去apache下吧 org.apache.commons.net这个包 源代码什么都有
    sun.net那个是sun的代码包