小弟,想用java语言编个在线的ftp程序,但不知道怎么入手,请各位大虾们指点迷津!小弟感激不尽啊

解决方案 »

  1.   

    去sun找sun.net包相关
    sun.net.ftp.FtpClient   
    sun.net.TelnetOutputStream   
      

  2.   

    import   java.io.*;   
      import   java.util.*;   
      import   java.net.*;   
      import   sun.net.ftp.FtpClient;   
      import   sun.net.TelnetOutputStream;   
        
      public   class   TestFTP   {   
        
        /**   The   host   name   of   the   FTP   server.   */   
        private   String   host   =   "somename";   
        
        /**   The   user   ID   to   login   to   the   FTP   server.   */   
        private   String   userID   =   "user";   
        
        /**   The   password   to   login   to   the   FTP   server.   */   
        private   String   password   =   "password";   
        
        /**   The   directory   on   the   FTP   server   to   upload   files   to.   */   
        private   String   directory   =   "filesdir";   
        
        /**   The   name   of   the   file   you   want   to   upload.   */   
        private   String   fileName   =   "somefile.doc";   
        
        public   static   void   main(String[]   args)   {   
            try   {   
                FtpClient   ftpClient   =   new   FtpClient();   
                ftpClient.openServer(host);   //   connect   to   FTP   server   
                ftpClient.login(userID,   password);   //   login   
                ftpClient.binary();   //   set   to   binary   mode   transfer   
                ftpClient.cd(directory);   //   change   directory   
                File   file   =   new   File(fileName);   
                TelnetOutputStream   out   =   ftpClient.put(file.getName());   
                FileInputStream   in   =   new   FileInputStream(file);   
                int   c   =   0;   
                while   ((c   =   in.read())   !=   -1   )   {   
                    out.write(c);   
                }   
                in.close();   
                out.close();   
                ftpClient.closeServer();   
            }   catch   (Exception   exception)   {   
                exception.printStackTrace();   
            }   
        }   
        
      }    
      

  3.   

    楼上您好,非得需要下这个包吗?能用socket编程实现吗?
      

  4.   

    肯定可以的,只要对FTP的包了解一下会解析就可以了,不过实现起来可能比较复杂,楼主加油- -!
      

  5.   

    只用socket当然可以,不过要熟悉FTP协议。好好研究一下RFC吧。
    http://www.faqs.org/rfcs/rfc959.html不过俺要打击一下LZ,FTP协议本身虽然非常简单,但是由于列目录等都是要自己解析,所以非常麻烦。
    比方说,要列出当前目录所有的文件和目录,由于不像File的list方法可以调用jni,FTP只是tcp/ip流,要解析起来非常麻烦。
    比方说下面这个就是list命令处理的流,你要从中解析出哪些是文件哪些目录,已经文件的更新时间,权限。
    特别是更新时间:半年以内Jan 31 12:56,半年以前Aug 02 2007,格式不一样,正则表达式很不好写。
    还有,不同语言的ftp服务器返回不用:英语Aug;中文八月,日语。drwx------   3 aaaa  bbbb           4096 Aug 02 2007  ccccc
    drwxr-xr-x   3 aaaa   aaaa        256 Mar 19 2007  dddddd
    drwx------   2 aaaa      aaaa           256 Jan 31 12:56 eeee
    drwxr-xr-x   2 aaaa    aaaa             256 Aug 26 2005  fff总之,很麻烦。
      

  6.   

    一样都是要自己解析字符串。用java还简单些。
      

  7.   

    随便一本java书上都有介绍的啊