基于Apache FtpServer 的Ftp服务器

    本文介绍通过spring 集成的方式来实现。创建spring-apacheFtpServer.xml文件如下图所示

 

 

<?xml version="1.0" encoding="UTF-8"?>
<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="apacheFtpServer">
	<listeners>
		<nio-listener name="default" port="21">
		</nio-listener>
	</listeners>
	<file-user-manager file="ftpConfig.properties" />
</server>

引入的属性文件ftpConfig.properties如下所示

 

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Password is "admin"
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
ftpserver.user.admin.homedirectory=F:\\temp
ftpserver.user.admin.enableflag=true
ftpserver.user.admin.writepermission=true
ftpserver.user.admin.maxloginnumber=0
ftpserver.user.admin.maxloginperip=0
ftpserver.user.admin.idletime=0
ftpserver.user.admin.uploadrate=0
ftpserver.user.admin.downloadrate=0

ftpserver.user.anonymous.userpassword=
ftpserver.user.anonymous.homedirectory=F:\\temp
ftpserver.user.anonymous.enableflag=true
ftpserver.user.anonymous.writepermission=false
ftpserver.user.anonymous.maxloginnumber=20
ftpserver.user.anonymous.maxloginperip=2
ftpserver.user.anonymous.idletime=300
ftpserver.user.anonymous.uploadrate=4800
ftpserver.user.anonymous.downloadrate=4800

 实现如下java代码。

 

package com.sdjxd.util;

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

import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.impl.DefaultFtpServer;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 项目启动的时候,就开启ftp服务器
 * 
 * @author zhen
 * 
 */
public class ApacheFtpServerListener implements ServletContextListener {
	private static final Logger logger = Logger
			.getLogger(ApacheFtpServerListener.class);
	private static final String FTPSERVER_CONTEXT_NAME = "FTPSERVER_CONTEXT_NAME";

	public void contextDestroyed(ServletContextEvent sce) {
		logger.info("正在停止FTP服务....");
		DefaultFtpServer server = (DefaultFtpServer) sce.getServletContext()
				.getAttribute(FTPSERVER_CONTEXT_NAME);
		if (server != null) {
			server.stop();
			sce.getServletContext().removeAttribute(FTPSERVER_CONTEXT_NAME);
			logger.info("FTP服务已停止!");
		} else {
			logger.info("停止FTP服务出错!");
		}
	}

	public void contextInitialized(ServletContextEvent sce) {
		logger.info("正在开启FTP服务....");
		ApplicationContext ac = WebApplicationContextUtils
				.getWebApplicationContext(sce.getServletContext());
		DefaultFtpServer server = (DefaultFtpServer) ac
				.getBean("apacheFtpServer");
		sce.getServletContext().setAttribute(FTPSERVER_CONTEXT_NAME, server);
		try {
			server.start();
			logger.info("FTP服务已开启!");
		} catch (FtpException e) {
			logger.error(e.getMessage());
		}
	}

}

 最后在web.xml文件中加入监听

 

	<listener>
		<description>apache ftp server监听器</description>
		<listener-class>com.sdjxd.util.ApacheFtpServerListener</listener-class>
	</listener>

 至此Ftp文件服务器架设成功,让我们用ftp客户端来测试一下我们的Ftp服务器吧。

 

注意 写道
Ftp客户端连接服务器编码要用utf-8,否则可能会出现路径或文件名乱码的情况!

 由于附件大小的限制,源码放到了CSDN,有需要的童鞋请到这里下载

FtpServer源码

;