基于springboot,使用ftpserver快速搭建一个FTP服务端。
搭建过程很简单,我们把过程分成4个步骤,一分钟内快速完成构建。
implementation 'org.apache.ftpserver:ftpserver-core:1.2.0'
<dependency>
<groupId>org.apache.ftpservergroupId>
<artifactId>ftpserver-coreartifactId>
<version>1.2.0version>
dependency>
首先,在springboot项目中增加两个配置,创建ftpserver.properties配置和在application.properties基础上增加ftp配置。配置如下:
#表示admin的密码是12345,以下都是admin的参数设置,可以设置多个用户
ftpserver.user.admin.userpassword=eguid
ftpserver.user.admin.homedirectory=C://ftp
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
#用户名是eguid的用户配置
ftpserver.user.eguid.userpassword=eguid
ftpserver.user.eguid.homedirectory=C://ftp/eguid
ftpserver.user.eguid.enableflag=true
ftpserver.user.eguid.writepermission=true
ftpserver.user.eguid.maxloginnumber=0
ftpserver.user.eguid.maxloginperip=0
ftpserver.user.eguid.idletime=0
ftpserver.user.eguid.uploadrate=0
ftpserver.user.eguid.downloadrate=0
在原有application.properties基础上增加以下配置
##FTP内嵌服务器
#ftp连接端口
ftp.port=8021
# ftp主动模式端口
ftp.activePort=8022
#ftp被动模式数据传输端口
ftp.passivePorts=50021-60021
该类继承自ApplicationRunner类,会在springboot服务启动后自动启动。
由于没有考虑到springboot重启的情况,所以ftp服务会出现无法释放端口的问题,解决该问题请参考博主另一篇文章《springboot内嵌ftpserver服务端在服务重启后报错端口被占用解决办法》,就可以解决跟随springboot重启ftp服务的问题。
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.ftplet.FtpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* 初始化ftp服务器
* @author 汤姆猫不是猫
*/
@Component
public class InitFtpServer implements ApplicationRunner {
public static final Logger log = LoggerFactory.getLogger(InitFtpServer.class);
@Autowired
private FtpServer server;
@Override
public void run(ApplicationArguments args) throws Exception {
try {
server.start();
log.info("汤姆猫不是猫: ftp server start success ");
} catch (FtpException e) {
e.printStackTrace();
log.error("汤姆猫不是猫: ftp server start error {}", e);
}
}
}
import org.apache.ftpserver.DataConnectionConfigurationFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import java.util.HashMap;
import java.util.Map;
/**
* ftpserver配置,含主动模式和被动模式
* @author 汤姆猫不是猫
*/
@Configuration
public class FtpServerConfig extends CachingConfigurerSupport {
@Value("${ftp.port}")
private Integer ftpPort;
@Value("${ftp.activePort}")
private Integer ftpActivePort;
@Value("${ftp.passivePorts}")
private String ftpPassivePorts;
@Bean
public FtpServer createFtpServer() {
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
//这个只是控制端口,还有主动模式端口和被动模式下的某个范围端口
factory.setPort(ftpPort);
factory.setIdleTimeout(10);
//被动模式
DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
dataConnectionConfigurationFactory.setIdleTime(30);
dataConnectionConfigurationFactory.setActiveLocalPort(ftpActivePort);//主动模式端口
dataConnectionConfigurationFactory.setPassiveIpCheck(true);
dataConnectionConfigurationFactory.setPassivePorts(ftpPassivePorts);被动模式使用的端口范围
factory.setDataConnectionConfiguration(dataConnectionConfigurationFactory.createDataConnectionConfiguration());
// 替换默认的监听器
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
try {
ClassPathResource classPathResource = new ClassPathResource("ftpserver.properties");
userManagerFactory.setUrl(classPathResource.getURL());
} catch (Exception e) {
throw new RuntimeException("汤姆猫不是猫:配置文件ftpserver.properties不存在");
}
userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
serverFactory.setUserManager(userManagerFactory.createUserManager());
Map<String, Ftplet> m = new HashMap<String, Ftplet>();
m.put("miaFtplet", new FtpServerListener());
serverFactory.setFtplets(m);
// 创建ftpserver
FtpServer server = serverFactory.createServer();
return server;
}
}
import org.apache.ftpserver.ftplet.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
* 监听ftpserver服务上传和下载情况
* @author 汤姆猫不是猫
*/
public class FtpServerListener extends DefaultFtplet {
public static final Logger log = LoggerFactory.getLogger(FtpServerListener.class);
/**
* 开始连接
*/
@Override
public FtpletResult onConnect(FtpSession session) throws FtpException,
IOException {
UUID sessionId = session.getSessionId();
if(sessionId!=null) {
log.info("{}尝试登录ftpserver", sessionId.toString());
}
User user = session.getUser();
if(user!=null&&user.getName()!=null){
log.info("{}尝试使用用户名:{},密码:{}登录ftpserver.", sessionId.toString(), user.getName(), user.getPassword());
}
return super.onConnect(session);
}
/**
* 关闭连接
*/
@Override
public FtpletResult onDisconnect(FtpSession session) throws FtpException,
IOException {
UUID sessionId = session.getSessionId();
if(sessionId!=null) {
log.info("{}关闭ftpserver连接", sessionId.toString());
}
User user = session.getUser();
if(user!=null&&user.getName()!=null){
log.info("{}用户名:{}关闭ftpserver连接.", sessionId.toString(), user.getName());
}
return super.onDisconnect(session);
}
/**
* 开始上传
*/
@Override
public FtpletResult onUploadStart(FtpSession session, FtpRequest request)
throws FtpException, IOException {
//获取上传文件的上传路径
String path = session.getUser().getHomeDirectory();
//自动创建上传路径
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
//获取上传用户
String name = session.getUser().getName();
//获取上传文件名
String filename = request.getArgument();
log.info("用户:'{}',上传文件到目录:'{}',文件名称为:'{}',状态:开始上传~", name, path, filename);
return super.onUploadEnd(session, request);
}
/**
* 上传完成
*/
@Override
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)
throws FtpException, IOException {
//获取上传文件的上传路径
String path = session.getUser().getHomeDirectory();
//获取上传用户
String name = session.getUser().getName();
//获取上传文件名
String filename = request.getArgument();
File file = new File(path + "/" + filename);
if (file.exists()) {
System.out.println(file);
}
log.info("用户:'{}',上传文件到目录:'{}',文件名称为:'{},状态:成功!'", name, path, filename);
return super.onUploadStart(session, request);
}
@Override
public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
return super.onDownloadStart(session, request);
}
@Override
public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
return super.onDownloadEnd(session, request);
}
}
以上步骤完成后,启动springboot。使用ftpclient工具尝试连接配置文件里的8021控制端口,如以下图所示,可以看到已经连接成功。
end
以上就是全部搭建过程,是不是很简单。如果觉得博主写得还不错,欢迎“关注、点赞、收藏”一键三连!