apache FtpServer 整合spring部署
我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这样通过ftp上传的文件会自动经过ftpServer来执行我们的逻辑判断,实现我们相应的功能!ftpServer是apache提供的纯java编写的Ftp服务器,能够方便的集成到J2EE项目中。采用这种集成方式无需在服务器端配置专门的FTP服务器。至于为什么要采用FTP服务器,是应一些大数据的上传所需。下面带领大家进入FtpServer的学习之旅1、下载相应的jar包,任选一种方式
apache官网版本包下载:http://mina.apache.org/ftpserver-project/downloads.html
本人博客jar包整理版下载:http://download.csdn.net/detail/harderxin/6319669
2、将相应的jar包部署到我们的web projects中
3、将log4j.properties和users.properties放到我们项目的src目录下
http://img.blog.csdn.net/20130925164433828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGFyZGVyWGlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
http://img.blog.csdn.net/20130925164455468?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGFyZGVyWGlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
上面只是ftp自带的一些配置
4、配置spring以及数据库连接池
${jdbc_driver} ${jdbc_url} ${jdbc_user} ${jdbc_password} 2 5、在application.xml(spring配置文件)添加Apache Ftpserver属性
6、编写我们的监听器,目的是操作FtpServer
package com.ftp.util; import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener; import org.apache.ftpserver.impl.DefaultFtpServer;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils; public class FtpServerListener implements ServletContextListener {public void contextDestroyed(ServletContextEvent contextEvent) {System.out.println("Stopping FtpServer");DefaultFtpServer server = (DefaultFtpServer) contextEvent.getServletContext().getAttribute("FTPSERVER_CONTEXT_NAME");if (server != null) {server.stop();contextEvent.getServletContext().removeAttribute("FTPSERVER_CONTEXT_NAME");System.out.println("FtpServer stopped");} else {System.out.println("No running FtpServer found");}} public void contextInitialized(ServletContextEvent contextEvent) {System.out.println("Starting FtpServer");WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(contextEvent.getServletContext());DefaultFtpServer server = (DefaultFtpServer) ctx.getBean("myServer"); contextEvent.getServletContext().setAttribute("FTPSERVER_CONTEXT_NAME", server); try {server.start();System.out.println("FtpServer started");} catch (Exception e) {throw new RuntimeException("Failed to start FtpServer", e);}}}
7、在web.xml中配置Spring和我们编写的监听器
contextConfigLocation classpath:applicationContext-*.xml org.springframework.web.context.ContextLoaderListener com.ftp.util.FtpServerListener index.jsp 这样我们的初步部署工作就完成了,如果启动不报错,说明我们配置成功!!接下来我们就要进行相应的逻辑监听处理了,见下篇文章--apache FtpServer 整合spring逻辑处理!!
页:
[1]