论坛首页 Java企业应用论坛

Apache FTPServer 集成到Spring WEB项目中

浏览 8158 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (11) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-10-19  

Apache FTPServer好东西,可以集成到自己的WEB项目中,单独用也非常不错。大家有兴趣可以研究一下,测试了一下和Spring WEB项目结合。留下代码如下:

 

web.xml

  <listener>
    <listener-class>com.strong.utils.ftpservice.FtpServerListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value> 
      /WEB-INF/classes/com/strong/spring/applicationFTP.xml
    </param-value>
  </context-param>

 

FtpServerListener.java

package com.strong.utils.ftpservice;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.ftpserver.FtpServer;
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 sce) {
    System.out.println("Stopping FtpServer");
    DefaultFtpServer server = (DefaultFtpServer) sce.getServletContext().getAttribute(FtpConstants.FTPSERVER_CONTEXT_NAME);
    if (server != null) {
      server.stop();
      sce.getServletContext().removeAttribute("FTPSERVER_CONTEXT_NAME");
      System.out.println("FtpServer stopped");
    } else {
      System.out.println("No running FtpServer found");
    }
  }

  public void contextInitialized(ServletContextEvent sce) {
    System.out.println("Starting FtpServer");
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    DefaultFtpServer server = (DefaultFtpServer) ctx.getBean("StrongFTP");
    sce.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);
    }
  }
}

 

applicationFTP.xml

<server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd"
  id="StrongFTP" max-logins="500" anon-enabled="false" max-anon-logins="123" max-login-failures="124" login-failure-delay="125">
  <listeners>
    <nio-listener name="default" port="2222" implicit-ssl="false" idle-timeout="60" local-address="127.0.0.1" />
  </listeners>
  <file-user-manager file="users.properties" encrypt-passwords="true" />
</server>

 具体参数就不解释了,大家看文档。大概的做法是做一个ServletContextListener,tomcat启动时开始ftp服务器,结束时停止ftp服务器。

   发表时间:2010-10-20  
学习了,值得收藏。
0 请登录后投票
   发表时间:2010-10-20  
好东西,看不太明白,慢慢领悟!
0 请登录后投票
   发表时间:2010-10-21  
一直找这方面的资料,谢谢 LZ 指路
不知道开了 Ftp 服务以后,java web 服务会不会很卡?
0 请登录后投票
   发表时间:2010-11-15   最后修改:2010-11-15
apache FtpServer 是mina的一个应用,和tomcat这些没有冲突。只要你给他的内存够大,不会影响到WEB服务器,当然如果因为服务器的性能不能满足,那肯定会影响WEB服务。

总的来说,你把这个集成到WEB应用中,和在同一台服务器上单独跑Apache FTPServer和WEB服务是一样的。

至少,我在实际的测试过程中没有发现,内部千兆网络中上传速度曾经达到过了100M+的峰值```````,是实际的文件上传速度不是bps哦
0 请登录后投票
   发表时间:2011-04-08  
请问LZ,单独用eclipse开发apache ftpserver怎么弄啊? apache ftpserver 一般都弄到web程序里面吗?  有什么区别没?
0 请登录后投票
   发表时间:2011-04-08  
FtpConstants 是哪里来的?
0 请登录后投票
   发表时间:2011-04-11  
applicationContext.xml这个用server 元素不会报错?
我用spring 1.2版本,难道用2.5的就不会报错?
0 请登录后投票
   发表时间:2011-04-14  
yoyo837 写道
FtpConstants 是哪里来的?

FtpConstants就是同目录下的一个常量文件而已,自己写就可以了
0 请登录后投票
   发表时间:2011-04-14  
kennethlin 写道
applicationContext.xml这个用server 元素不会报错?
我用spring 1.2版本,难道用2.5的就不会报错?

这个我还真没注意,反正就跑起来了,应该是命名空间的问题把
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics