apacheFtpServer集成spring由web容器控制启动和关闭

ApacheFtpServer是一个100%纯Java实现的FTP服务器,基于网络框架apache MINA实现,可支撑多并发用户。FtpServer可以独立运行作为一个Windows服务或Unix/Linux守护进程,或嵌入到Java应用程序,提供对内部集成spring应用程序支持。下面介绍apacheFtpServer与spring集成,交由spring控制ApacheFtpServer的启动与关闭。

1.      初始化创建MyFtpServer:

import java.io.File;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
importorg.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
 
public class MyFtpServer {
       FtpServer server =null;
       public void initFtp(){
             FtpServerFactoryserverFactory = new FtpServerFactory();
             ListenerFactorylistenerFactory = new ListenerFactory();
              // replacethedefault listener
             listenerFactory.setPort(21);
             serverFactory.addListener("default",listenerFactory.createListener());
             PropertiesUserManagerFactoryuserManagerFactory = newPropertiesUserManagerFactory();
             userManagerFactory.setFile(newFile(          "D:\\stcaimis\\apache-ftpserver-1.0.3\\res\\conf\\users2.properties"));
              //设置盐化hash加密算法
             //SaltedPasswordEncryptorsalterpassword = new SaltedPasswordEncryptor();
            //userManagerFactory.setPasswordEncryptor(salterpassword);
             //设定了用户输入密码时 直接输入明文
             userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());                
             serverFactory.setUserManager(userManagerFactory.createUserManager());
              server=serverFactory.createServer();       
       }
     
       public void start(){             
              try {
                    server.start();
                    System.out.println("serverstarting");
              }catch(FtpException e) {
                     //TODOAuto-generated catch block
                    e.printStackTrace();
              }
       }
       public void stop() {
              server.stop();
             System.out.println("serveris stopped");
       }   
}


spring 监听器在启动Web 容器时,自动装配SpringapplicationContext.xml 的配置信息。在application-context.xml文件中配置myFtpServer javabean

 <beanid="StrongFTP"class="com.st.caims.lxj.test.MyFtpServer"></bean>

2. web.xml配置ftpListener,自定义监听器FtpServerListener implements ServletContextListener接口,启动web容器时,自动执行contextInitialized()方法,web容器关闭时调用contextDestroyed()方法。

 

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.ftpserver.impl.DefaultFtpServer;
import org.springframework.web.context.WebApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
 
public class FtpServerListener implementsServletContextListener{ 
    //tomcat容器关闭时调用方法stop ftpServer
  public voidcontextDestroyed(ServletContextEvent sce) {  
         WebApplicationContextctx=WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); 
        MyFtpServerserver=(MyFtpServer)ctx.getServletContext().getAttribute("FTPSERVER_CONTEXT_NAME");
    server.stop();
  sce.getServletContext().removeAttribute("FTPSERVER_CONTEXT_NAME");
  System.out.println("Stopping FtpServer");
  }
 
  //spring 容器初始化调用方法startFtpServer
  public voidcontextInitialized(ServletContextEvent sce) { 
  System.out.println("Starting FtpServer"); 
    WebApplicationContextctx=WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); 
    MyFtpServer server=(MyFtpServer) ctx.getBean("StrongFTP");
   sce.getServletContext().setAttribute("FTPSERVER_CONTEXT_NAME",server); 
    try {         
           server.initFtp();
           server.start();
    System.out.println("FtpServer started"); 
    } catch (Exceptione){ 
      thrownewRuntimeException("Failed to start FtpServer", e); 
    } 
  } 
}

 

ps: Apache FtpServer 官方下载地址:http://mina.apache.org/,API文档。


apacheFtpServer集成spring由web容器控制启动和关闭,布布扣,bubuko.com

apacheFtpServer集成spring由web容器控制启动和关闭

上一篇:《javascript语言精粹》——第3章


下一篇:使用spring注解,注入sessionFactory类