ITeye博客

Apache FTPServer 集成到Spring WEB项目中

simen_net · 2010-10-19 17:04 · Spring · 企业架构 · Markdown

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服务器。

评论(10)

  1. #10 楼 · simen_net · 2011-04-14 23:14

    yoyo837 writes:

    请问LZ,单独用eclipse开发apache ftpserver怎么弄啊? apache ftpserver 一般都弄到web程序里面吗? 有什么区别没?

    单独跑FTPServer最简单了,官方的文档就是这样弄的,还可以开机自启动。

  2. #9 楼 · simen_net · 2011-04-14 23:13

    kennethlin writes:

    applicationContext.xml这个用server 元素不会报错? 我用spring 1.2版本,难道用2.5的就不会报错?

    这个我还真没注意,反正就跑起来了,应该是命名空间的问题把

  3. #8 楼 · simen_net · 2011-04-14 23:12

    yoyo837 writes:

    FtpConstants 是哪里来的?

    FtpConstants就是同目录下的一个常量文件而已,自己写就可以了

  4. #7 楼 · kennethlin · 2011-04-11 12:13

    applicationContext.xml这个用server 元素不会报错? 我用spring 1.2版本,难道用2.5的就不会报错?

  5. #6 楼 · yoyo837 · 2011-04-08 16:11

    FtpConstants 是哪里来的?

  6. #5 楼 · yoyo837 · 2011-04-08 15:55

    请问LZ,单独用eclipse开发apache ftpserver怎么弄啊? apache ftpserver 一般都弄到web程序里面吗? 有什么区别没?

  7. #4 楼 · simen_net · 2010-11-15 11:05

    apache FtpServer 是mina的一个应用,和tomcat这些没有冲突。只要你给他的内存够大,不会影响到WEB服务器,当然如果因为服务器的性能不能满足,那肯定会影响WEB服务。

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

    至少,我在实际的测试过程中没有发现,内部千兆网络中上传速度曾经达到过了100M+的峰值```````,是实际的文件上传速度不是bps哦

  8. #3 楼 · wuliupo · 2010-10-21 23:08

    一直找这方面的资料,谢谢 LZ 指路 不知道开了 Ftp 服务以后,java web 服务会不会很卡?

  9. #2 楼 · lshoo · 2010-10-20 18:29

    好东西,看不太明白,慢慢领悟!

  10. #1 楼 · zhonggeneral · 2010-10-20 08:55

    学习了,值得收藏。

评论功能维护中,当前仅展示历史评论。