2012-01-23 55 views
11

我打算寫一個代碼,在我的立場上打開一個ftp服務器,所以我可以將文件從它複製到另一臺計算機上的客戶端和相反,但我很新的服務器端編程並不明白如何...寫一個java ftp服務器

我得到了Apache FtpServer,但有點與它的使用混淆,所以如果有人可以幫助我的基本步驟,我將非常感激。
也許是這樣的:

  1. 不連接命令
  2. 登錄
  3. 做一些事情....
+1

代碼是否我寫的對你的工作? –

回答

31

讓我寫一個基本的例子給你,使用非常有用阿帕奇FtpServer

FtpServerFactory serverFactory = new FtpServerFactory(); 
ListenerFactory factory = new ListenerFactory(); 
factory.setPort(1234);// set the port of the listener (choose your desired port, not 1234) 
serverFactory.addListener("default", factory.createListener()); 
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); 
userManagerFactory.setFile(new File("/home/blablah/myusers.properties"));//choose any. We're telling the FTP-server where to read it's user list 
userManagerFactory.setPasswordEncryptor(new PasswordEncryptor() 
{//We store clear-text passwords in this example 

     @Override 
     public String encrypt(String password) { 
      return password; 
     } 

     @Override 
     public boolean matches(String passwordToCheck, String storedPassword) { 
      return passwordToCheck.equals(storedPassword); 
     } 
    }); 
    //Let's add a user, since our myusers.properties files is empty on our first test run 
    BaseUser user = new BaseUser(); 
    user.setName("test"); 
    user.setPassword("test"); 
    user.setHomeDirectory("/home/blablah"); 
    List<Authority> authorities = new ArrayList<Authority>(); 
    authorities.add(new WritePermission()); 
    user.setAuthorities(authorities); 
    UserManager um = userManagerFactory.createUserManager(); 
    try 
    { 
     um.save(user);//Save the user to the user list on the filesystem 
    } 
    catch (FtpException e1) 
    { 
     //Deal with exception as you need 
    } 
    serverFactory.setUserManager(um); 
    Map<String, Ftplet> m = new HashMap<String, Ftplet>(); 
    m.put("miaFtplet", new Ftplet() 
    { 

     @Override 
     public void init(FtpletContext ftpletContext) throws FtpException { 
      //System.out.println("init"); 
      //System.out.println("Thread #" + Thread.currentThread().getId()); 
     } 

     @Override 
     public void destroy() { 
      //System.out.println("destroy"); 
      //System.out.println("Thread #" + Thread.currentThread().getId()); 
     } 

     @Override 
     public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException 
     { 
      //System.out.println("beforeCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine()); 
      //System.out.println("Thread #" + Thread.currentThread().getId()); 

      //do something 
      return FtpletResult.DEFAULT;//...or return accordingly 
     } 

     @Override 
     public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException 
     { 
      //System.out.println("afterCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine() + " | " + reply.getMessage() + " : " + reply.toString()); 
      //System.out.println("Thread #" + Thread.currentThread().getId()); 

      //do something 
      return FtpletResult.DEFAULT;//...or return accordingly 
     } 

     @Override 
     public FtpletResult onConnect(FtpSession session) throws FtpException, IOException 
     { 
      //System.out.println("onConnect " + session.getUserArgument() + " : " + session.toString()); 
      //System.out.println("Thread #" + Thread.currentThread().getId()); 

      //do something 
      return FtpletResult.DEFAULT;//...or return accordingly 
     } 

     @Override 
     public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException 
     { 
      //System.out.println("onDisconnect " + session.getUserArgument() + " : " + session.toString()); 
      //System.out.println("Thread #" + Thread.currentThread().getId()); 

      //do something 
      return FtpletResult.DEFAULT;//...or return accordingly 
     } 
    }); 
    serverFactory.setFtplets(m); 
    //Map<String, Ftplet> mappa = serverFactory.getFtplets(); 
    //System.out.println(mappa.size()); 
    //System.out.println("Thread #" + Thread.currentThread().getId()); 
    //System.out.println(mappa.toString()); 
    FtpServer server = serverFactory.createServer(); 
    try 
    { 
     server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set 
    } 
    catch (FtpException ex) 
    { 
     //Deal with exception as you need 
    } 

請注意,在服務器端,您不必手動處理連接,登錄等:Ftplet會爲您執行此操作。

你可以,但是,添加自定義的預[或交]您的匿名內部類Ftplet的重載方法中 - 處理(當你new Ftplet(){ ... }實例化。

+0

'FtpServerFactory'從哪裏來? – 2012-01-23 10:24:11

+0

它來自Apache FtpServer核心(OP在他的問題中聲明他得到了Apache FtpServer API,但不知道從哪裏開始)不管怎樣,我忘了明確表示我在使用Apache FtpServer回答;我馬上就會,謝謝你的評論。 –

+1

它的工作!! :)非常感謝! – moshe