职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1542|回复: 8

Apache FTPServer 集成到Spring WEB项目中

[复制链接]
hxy 发表于 2011-9-3 10:44 | 显示全部楼层 |阅读模式
Apache FTPServer好东西,可以集成到自己的WEB项目中,单独用也非常不错。大家有兴趣可以研究一下,测试了一下和Spring WEB项目结合。留下代码如下:

web.xml
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>  

  <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);   
    }   
  }   
}  

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
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>  

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

feiguo 发表于 2011-9-3 10:44 | 显示全部楼层
学习了,值得收藏。

秋秋 发表于 2011-9-3 10:44 | 显示全部楼层
好东西,看不太明白,慢慢领悟!

曾经的小孩 发表于 2011-9-3 10:44 | 显示全部楼层
一直找这方面的资料,谢谢 LZ 指路
不知道开了 Ftp 服务以后,java web 服务会不会很卡?

叫我小乖 发表于 2011-9-3 10:45 | 显示全部楼层
请问LZ,单独用eclipse开发apache ftpserver怎么弄啊? apache ftpserver 一般都弄到web程序里面吗?  有什么区别没?

无处不在 发表于 2011-9-3 10:45 | 显示全部楼层
FtpConstants 是哪里来的?

北大青鸟 发表于 2011-9-3 10:45 | 显示全部楼层
applicationContext.xml这个用server 元素不会报错?
我用spring 1.2版本,难道用2.5的就不会报错?

gz-vps 发表于 2011-9-3 10:45 | 显示全部楼层
yoyo837 写道
FtpConstants 是哪里来的?

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

走就走吧 发表于 2011-9-3 10:45 | 显示全部楼层
kennethlin 写道
applicationContext.xml这个用server 元素不会报错?
我用spring 1.2版本,难道用2.5的就不会报错?

这个我还真没注意,反正就跑起来了,应该是命名空间的问题把
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-3 12:29 , Processed in 0.128606 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表