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