- 浏览: 2488233 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
APACHE基于MINA的FTP SERVER(二)服务器初始化
需要引入的JAR包
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
web.xml配置启动时调用spring初始化
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
spring配置调用核心类FtpServerServiceImpl初始化FTP的SERVER池
<beans default-autowire="byName">
<bean name="ftpServerService" class="com.sillycat.easyftp.server.impl.FtpServerServiceImpl"
init-method="init">
<property name="configFile" value="${ftpserver.config.file}" />
</bean>
</beans>
单元测试中
public void testStop() {
FtpServerService ftpServerService = (FtpServerService) appContext.getBean("ftpServerService");
//停止名字为ftpsillycat的server
ftpServerService.stop("ftpsillycat");
}
服务器的初始化的核心类FtpServerServiceImpl.java,代码如下:
package com.sillycat.easyftp.server.impl;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.stream.XMLInputFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.Md5PasswordEncryptor;
import org.apache.ftpserver.usermanager.PasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import com.sillycat.easybase.base.BaseManager;
import com.sillycat.easybase.constants.FtpConstants;
import com.sillycat.easyftp.config.FtpServerConfig;
import com.sillycat.easyftp.config.UserServerConfig;
import com.sillycat.easyftp.server.FtpServerService;
import com.sillycat.easyftp.utils.XmlResourceUtil;
public class FtpServerServiceImpl extends BaseManager implements
FtpServerService {
//ftp服务器的池
private ConcurrentHashMap<String, FtpServer> ftpServerPool;
//配置集合
private List<FtpServerConfig> ftpServerConfigs;
//读取的XML配置文件
private String configFile;
//启动服务器
public void start(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
try {
ftpServer.start();
} catch (FtpException e) {
e.printStackTrace();
}
}
//停止服务器
public void stop(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
ftpServer.stop();
}
//重启服务器
public void resume(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
ftpServer.resume();
}
//暂停服务器
public void suspend(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
ftpServer.suspend();
}
public void init() {
// 初始化变量
ftpServerPool = new ConcurrentHashMap<String, FtpServer>();
ftpServerConfigs = new ArrayList<FtpServerConfig>();
// 装在配置文件
loadConfig();
if (ftpServerConfigs != null && !ftpServerConfigs.isEmpty()) {
initFtpServerPool();
} else {
logger.error("no config info for FtpServerServiceImpl!");
throw new RuntimeException(
"no config info for FtpServerServiceImpl!");
}
}
//初始化FTP服务器池
public void initFtpServerPool() {
for (int i = 0; i < ftpServerConfigs.size(); i++) {
FtpServerConfig ftpServerConfig = ftpServerConfigs.get(i);
this.addFtpServer(ftpServerConfig);
}
if(ftpServerPool != null && !ftpServerPool.isEmpty()){
Iterator<String> itKey = ftpServerPool.keySet().iterator();
for(;itKey.hasNext();){
String key = itKey.next();
FtpServer tmp = ftpServerPool.get(key);
if(tmp != null){
try {
tmp.start();
logger.info("ftp server " + key + " start!");
} catch (FtpException e) {
e.printStackTrace();
}
}
}
}
}
//读取配置文件
public void loadConfig() {
URL url = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
url = loader.getResource(this.getConfigFile());
XMLInputFactory factory = XMLInputFactory.newInstance();
if (url == null) {
logger
.error("no ftpserver config find! please put ftpserver.xml in your classpath");
throw new java.lang.RuntimeException(
"no ftpserver config find! please put ftpserver.xml in your classpath");
}
XmlResourceUtil.loadFtpServerConfigFromURL(url, factory,
ftpServerConfigs);
logger.info(new StringBuffer().append("load config from :").append(
url.getFile()));
}
private void addFtpServer(FtpServerConfig ftpServerConfig) {
FtpServerFactory ftpServerFactory = new FtpServerFactory();
// 设置监听端口
ListenerFactory linstenFactory = new ListenerFactory();
linstenFactory.setPort(ftpServerConfig.getPort());
ftpServerFactory
.addListener("default", linstenFactory.createListener());
// 密码加密类型
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
PasswordEncryptor passwordEncryptor = null;
if (FtpConstants.CLEAR_TEXT_FTP_PASSWORD_ENCRYPTOR
.equalsIgnoreCase(ftpServerConfig.getPasswordEncryptor())) {
passwordEncryptor = new ClearTextPasswordEncryptor();
} else if (FtpConstants.SALTED_FTP_PASSWORD_ENCRYPTOR
.equalsIgnoreCase(ftpServerConfig.getPasswordEncryptor())) {
passwordEncryptor = new SaltedPasswordEncryptor();
} else if (FtpConstants.MD5_FTP_PASSWORD_ENCRYPTOR
.equalsIgnoreCase(ftpServerConfig.getPasswordEncryptor())) {
passwordEncryptor = new Md5PasswordEncryptor();
}
userManagerFactory.setPasswordEncryptor(passwordEncryptor);
// 用户
UserManager um = userManagerFactory.createUserManager();
if (ftpServerConfig.getUsers() != null
&& !ftpServerConfig.getUsers().isEmpty()) {
for (int i = 0; i < ftpServerConfig.getUsers().size(); i++) {
UserServerConfig tmp = ftpServerConfig.getUsers().get(i);
addFtpUser(tmp, um);
}
}
ftpServerFactory.setUserManager(um);
FtpServer server = ftpServerFactory.createServer();
ftpServerPool.put(ftpServerConfig.getName(), server);
}
private void addFtpUser(UserServerConfig userServerConfig, UserManager um) {
BaseUser user = new BaseUser();
user.setName(userServerConfig.getName());
user.setPassword(userServerConfig.getPassword());
user.setHomeDirectory(userServerConfig.getHomeDirectory());
user.setEnabled(userServerConfig.getEnabled());
user.setMaxIdleTime(userServerConfig.getMaxIdleTime());
if (userServerConfig.getPermissions() != null
&& !userServerConfig.getPermissions().isEmpty()) {
//权限
user.setAuthorities(userServerConfig.getPermissions());
}
try {
um.save(user);
} catch (FtpException e) {
e.printStackTrace();
}
}
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
}
需要引入的JAR包
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
web.xml配置启动时调用spring初始化
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
spring配置调用核心类FtpServerServiceImpl初始化FTP的SERVER池
<beans default-autowire="byName">
<bean name="ftpServerService" class="com.sillycat.easyftp.server.impl.FtpServerServiceImpl"
init-method="init">
<property name="configFile" value="${ftpserver.config.file}" />
</bean>
</beans>
单元测试中
public void testStop() {
FtpServerService ftpServerService = (FtpServerService) appContext.getBean("ftpServerService");
//停止名字为ftpsillycat的server
ftpServerService.stop("ftpsillycat");
}
服务器的初始化的核心类FtpServerServiceImpl.java,代码如下:
package com.sillycat.easyftp.server.impl;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.stream.XMLInputFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.Md5PasswordEncryptor;
import org.apache.ftpserver.usermanager.PasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import com.sillycat.easybase.base.BaseManager;
import com.sillycat.easybase.constants.FtpConstants;
import com.sillycat.easyftp.config.FtpServerConfig;
import com.sillycat.easyftp.config.UserServerConfig;
import com.sillycat.easyftp.server.FtpServerService;
import com.sillycat.easyftp.utils.XmlResourceUtil;
public class FtpServerServiceImpl extends BaseManager implements
FtpServerService {
//ftp服务器的池
private ConcurrentHashMap<String, FtpServer> ftpServerPool;
//配置集合
private List<FtpServerConfig> ftpServerConfigs;
//读取的XML配置文件
private String configFile;
//启动服务器
public void start(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
try {
ftpServer.start();
} catch (FtpException e) {
e.printStackTrace();
}
}
//停止服务器
public void stop(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
ftpServer.stop();
}
//重启服务器
public void resume(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
ftpServer.resume();
}
//暂停服务器
public void suspend(String name) {
FtpServer ftpServer = ftpServerPool.get(name);
ftpServer.suspend();
}
public void init() {
// 初始化变量
ftpServerPool = new ConcurrentHashMap<String, FtpServer>();
ftpServerConfigs = new ArrayList<FtpServerConfig>();
// 装在配置文件
loadConfig();
if (ftpServerConfigs != null && !ftpServerConfigs.isEmpty()) {
initFtpServerPool();
} else {
logger.error("no config info for FtpServerServiceImpl!");
throw new RuntimeException(
"no config info for FtpServerServiceImpl!");
}
}
//初始化FTP服务器池
public void initFtpServerPool() {
for (int i = 0; i < ftpServerConfigs.size(); i++) {
FtpServerConfig ftpServerConfig = ftpServerConfigs.get(i);
this.addFtpServer(ftpServerConfig);
}
if(ftpServerPool != null && !ftpServerPool.isEmpty()){
Iterator<String> itKey = ftpServerPool.keySet().iterator();
for(;itKey.hasNext();){
String key = itKey.next();
FtpServer tmp = ftpServerPool.get(key);
if(tmp != null){
try {
tmp.start();
logger.info("ftp server " + key + " start!");
} catch (FtpException e) {
e.printStackTrace();
}
}
}
}
}
//读取配置文件
public void loadConfig() {
URL url = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
url = loader.getResource(this.getConfigFile());
XMLInputFactory factory = XMLInputFactory.newInstance();
if (url == null) {
logger
.error("no ftpserver config find! please put ftpserver.xml in your classpath");
throw new java.lang.RuntimeException(
"no ftpserver config find! please put ftpserver.xml in your classpath");
}
XmlResourceUtil.loadFtpServerConfigFromURL(url, factory,
ftpServerConfigs);
logger.info(new StringBuffer().append("load config from :").append(
url.getFile()));
}
private void addFtpServer(FtpServerConfig ftpServerConfig) {
FtpServerFactory ftpServerFactory = new FtpServerFactory();
// 设置监听端口
ListenerFactory linstenFactory = new ListenerFactory();
linstenFactory.setPort(ftpServerConfig.getPort());
ftpServerFactory
.addListener("default", linstenFactory.createListener());
// 密码加密类型
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
PasswordEncryptor passwordEncryptor = null;
if (FtpConstants.CLEAR_TEXT_FTP_PASSWORD_ENCRYPTOR
.equalsIgnoreCase(ftpServerConfig.getPasswordEncryptor())) {
passwordEncryptor = new ClearTextPasswordEncryptor();
} else if (FtpConstants.SALTED_FTP_PASSWORD_ENCRYPTOR
.equalsIgnoreCase(ftpServerConfig.getPasswordEncryptor())) {
passwordEncryptor = new SaltedPasswordEncryptor();
} else if (FtpConstants.MD5_FTP_PASSWORD_ENCRYPTOR
.equalsIgnoreCase(ftpServerConfig.getPasswordEncryptor())) {
passwordEncryptor = new Md5PasswordEncryptor();
}
userManagerFactory.setPasswordEncryptor(passwordEncryptor);
// 用户
UserManager um = userManagerFactory.createUserManager();
if (ftpServerConfig.getUsers() != null
&& !ftpServerConfig.getUsers().isEmpty()) {
for (int i = 0; i < ftpServerConfig.getUsers().size(); i++) {
UserServerConfig tmp = ftpServerConfig.getUsers().get(i);
addFtpUser(tmp, um);
}
}
ftpServerFactory.setUserManager(um);
FtpServer server = ftpServerFactory.createServer();
ftpServerPool.put(ftpServerConfig.getName(), server);
}
private void addFtpUser(UserServerConfig userServerConfig, UserManager um) {
BaseUser user = new BaseUser();
user.setName(userServerConfig.getName());
user.setPassword(userServerConfig.getPassword());
user.setHomeDirectory(userServerConfig.getHomeDirectory());
user.setEnabled(userServerConfig.getEnabled());
user.setMaxIdleTime(userServerConfig.getMaxIdleTime());
if (userServerConfig.getPermissions() != null
&& !userServerConfig.getPermissions().isEmpty()) {
//权限
user.setAuthorities(userServerConfig.getPermissions());
}
try {
um.save(user);
} catch (FtpException e) {
e.printStackTrace();
}
}
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
}
发表评论
-
Update Site will come soon
2021-06-02 04:10 1613I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 381Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 378Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 329Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 403VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 302Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 347PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 600Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 231Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 258Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 204MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 251MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 286Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 272Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 293Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 240Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 289K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 325Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 404Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 336Redis Cluster 2019(3)Redis Clus ...
相关推荐
Apache_Mina_Server_ 深入教程V1.0Apache_Mina_Server_ 深入教程V1.0Apache_Mina_Server_ 深入教程V1.0Apache_Mina_Server_ 深入教程V1.0Apache_Mina_Server_ 深入教程V1.0
Apache_Mina-FtpServer_use 从下载Mina FTP 服务器进步SFTP 档案监控完全的帐户管理端口更改
Apache FtpServer是一个100%纯Java的、基于现有开放式协议基础上、完整、小巧的FTP服务器。此外,FtpServer还可以作为Windows服务器、Unix / Linux后台程序或是被嵌入在Java应用程序而独立运行。有了MINA...
Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)
深入理解Apache_Mina_(1)----_Mina的几个类 深入理解Apache_Mina_(2)----_与IoFilter相关的几个类 深入理解Apache_Mina_(3)----_与IoHandler相关的几个类 深入理解Apache_Mina_(4)----_IoFilter和IoHandler的区别和...
Apache_Mina_Server_2.0中文参考手册,Apache_Mina_Server_2.0中文参考手册,Apache_Mina_Server_2.0中文参考手册
Apache_Mina_Server中文参考手册,简单易用清晰的说明
Apache_Mina_Server_2.0中文参考手册
Apache_Mina_Server_ 深入教程V1.0.pdf
Apache_Mina_Server_2.0中文参考手册V1.0.pdf
Apache_Mina_Server_2.0中文参考手册V1.0 中文Mina实用帮助手册
ftpserver mina框架,适合初学。
Apache_Mina_Server_2.0中文参考手册V1.0
Apache MINA基于JAVA的网络服务器框架 教程.zip
Apache_Mina_Server_2.0中文参考手册V1.0.pdf .
Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP、UDP/IP协议栈的通信框架(当然,也可以提供JAVA 对象的序列化服务、虚拟机管道通信服务等),Mina 可以帮助我们快速开发高性能、高...
Apache FtpServer是100%纯Java FTP服务器。它被设计为基于当前可用的开放协议的完整且 可移植的FTP服务器引擎解决方案。FtpServer可以作为Windows服务或Unix / Linux守护程序独立运行, 也可以嵌入Java应用程序中...