apacheFtpServer集成spring由web容器控制启动和关闭
来源:互联网 发布:网络拓扑结构概念 编辑:程序博客网 时间:2024/06/02 23:15
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文档。
0 0
- apacheFtpServer集成spring由web容器控制启动和关闭
- spring容器启动事件和关闭事件
- web容器启动spring容器
- web容器启动Spring
- spring 容器启动和关闭执行某个动作 ApplicationContext
- 【Spring学习25】容器级启动和关闭回调
- WebService 学习笔记之 集成Spring和Web容器
- web项目启动Spring容器
- Web容器 与Spring容器 启动
- 监听Web容器启动与关闭
- spring容器事件(启动,关闭,刷新...)
- spring(2) spring ioc容器在Web容器中的启动
- Web容器启动时加载Spring
- Spring 学习-------WebApplication (web 容器)启动
- Web容器启动时加载Spring分析
- web是如何启动spring容器的?
- web是如何启动spring容器的
- Spring在Web容器中的启动过程
- cloud foundry的组件
- ffmpeg x264 linux
- 关于巴拿赫-塔斯基分球定理的研究
- getchar()与getch(){isatty(),tcgetattr(),tcsetattr()}
- Windows下搭建PHP开发环境
- apacheFtpServer集成spring由web容器控制启动和关闭
- 代理模式--学习笔记
- 芝士披萨的做法
- 获取手机sdcard大小
- 怎样选择好书?
- ReplaceArrayWithObject(以对象取代数组)
- 第7章 网络连接存储
- Unity3D C# Thread通过JNI调用安卓接口出错
- 那一年,桃雨微靡