一个纯java的ftp服务器——apache ftpserver

项目地址http://mina.apache.org/ftpserver/

1、底层通信使用mina

2、支持ssl认证

3、支持主动与被动模式

4、用户管理可以基于文件,也可以基于数据库,非常的方便

5、可以独立启动,也可方便的潜入到其他应用中,

6、官方提供了一个与spring集成的例子,还有一个osgibundle的例子

官方提供的spring集成的例子使用的是扩展springNamespaceHandler的方式,由于我们的一个应用还是使用的较老版本的spring,对schema的支持还有些问题,所以我将其转换成了传统的spring配置方式

<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://toolkit.alibaba-inc.com/dtd/spring/spring-beans.dtd">

<beans default-autowire="byName">
	<!-- FtpServer -->
	<bean id="ftpServer" factory-bean="ftpServerFactory" factory-method="createServer" />
	<bean id="ftpServerFactory" class="org.apache.ftpserver.FtpServerFactory">
	 	<property name="userManager" ref="propertiesUserManager"/>
		<property name="connectionConfig" ref="connectionConfig"/>
		<property name="listeners">
			<map>
				<entry key="listeners" value-ref="nioListener" />
			</map>
        </property>
	</bean>
	
	<!-- PropertiesUserManager -->
	<bean id="propertiesUserManagerFactory" class="org.apache.ftpserver.usermanager.PropertiesUserManagerFactory">
		<property name="adminName" value="admin"/>
		<property name="file" value="${ftpserver_user_properties_path}"/>
		<property name="passwordEncryptor">
			<bean class="org.apache.ftpserver.usermanager.Md5PasswordEncryptor" />
		</property>
	</bean>
	<bean id="propertiesUserManager" factory-bean="propertiesUserManagerFactory" factory-method="createUserManager" />
	
	<!-- ConnectionConfig -->
	<bean id="connectionConfigFactory" class="org.apache.ftpserver.ConnectionConfigFactory">
		<property name="maxLogins" value="20"/>
		<property name="anonymousLoginEnabled" value="true"/>
		<property name="maxAnonymousLogins" value="20"/>
		<property name="maxLoginFailures" value="3"/>
		<property name="loginFailureDelay" value="500"/>
		<property name="maxThreads" value="0"/>
	</bean>
	<bean id="connectionConfig" factory-bean="connectionConfigFactory" factory-method="createConnectionConfig" />
	
	<!-- Listener -->
	<bean id="nioListenerFactory" class="org.apache.ftpserver.listener.ListenerFactory">
		<property name="port" value="2121"/>
	</bean>
	<bean id="nioListener" factory-bean="nioListenerFactory" factory-method="createListener" />

	<!--定义FTP SERVER Launcher-->
	<bean id="ftpServerLauncher" class="com.taobao.dw.query.ftp.server.FtpServerLauncher" init-method="startup" destroy-method="shutdown">
 		<property name="ftpServer" ref="ftpServer" />
	</bean>
</beans>

以下是ftp启动类:

package com.igoolu.ftp.server;

import org.apache.ftpserver.FtpServer;


/**
 * Ftp Server的启动与关闭
 *
 * @author summersun_ym
 * @version $Id: FtpServerListener1.java 2010-11-22 上午09:47:00 $
 *
 */
public class FtpServerLauncher {
    /** logger */
    protected final Logger log = LoggerFactory.getLogger(getClass());
    
    FtpServer ftpServer;
    
    public void setFtpServer(FtpServer ftpServer) {
        this.ftpServer = ftpServer;
    }

    /**
     * 停止FTP SERVER
     * 
     * @param sce
     */
    public void shutdown() {
        log.info("Stopping FtpServer");
        if (ftpServer != null) {
            ftpServer.stop();
            log.info("FtpServer stopped");
        } else {
            log.info("No running FtpServer found");
        }
    }
    
    /**
     * 启动FTP SERVER
     * 
     * @param sce
     */
    public void startup() {
        log.info("Starting FtpServer");
        try {
            ftpServer.start();
            log.info("FtpServer started");
        } catch (Exception e) {
            throw new RuntimeException("Failed to start FtpServer", e);
        }
    }
}