使用apache的包
@Component
@Slf4j
public class FmsFtpServer {
@Autowired
private SystemConfig sysConfig;
public void startFtp() {
try {
String ftpPath = sysConfig.getRootPath();//# rootpath: /stsvc/fms/data/
File path = new File(ftpPath);
if (!path.exists()) {
path.mkdirs();
log.info("success to create ftp path:{}", ftpPath);
}
createFtpServer().start();
log.info("success to start ftpServer.");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("FTP启动失败", e);
}
}
private FtpServer createFtpServer() {
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory listenerFactory = new ListenerFactory();
listenerFactory.setPort(sysConfig.getFtpPort());
//设置被动模式数据上传的接口范围,云服务器需要开放对应区间的端口给客户端
DataConnectionConfigurationFactory dataConnectionConfFactory = new DataConnectionConfigurationFactory();
dataConnectionConfFactory.setPassivePorts(sysConfig.getFtpPassivePorts());
listenerFactory.setDataConnectionConfiguration(dataConnectionConfFactory.createDataConnectionConfiguration());
serverFactory.addListener("default", listenerFactory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File("users.properties"));
userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
serverFactory.setUserManager(userManagerFactory.createUserManager());
return serverFactory.createServer();
}