引入依赖

<!-- https://mvnrepository.com/artifact/org.apache.ftpserver/ftpserver-core -->
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.1.1</version>
</dependency>

UploadListener.java

import org.apache.ftpserver.ftplet.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.File;
import java.io.IOException; public class UploadListener extends DefaultFtplet { public static final Logger log= LoggerFactory.getLogger(UploadListener.class); /**
*
* 开始上传
* Override this method to intercept uploads
* @param session The current {@link FtpSession}
* @param request The current {@link FtpRequest}
* @return The action for the container to take
* @throws FtpException
* @throws IOException
*/
@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 this method to handle uploads after completion
* @param session The current {@link FtpSession}
* @param request The current {@link FtpRequest}
* @return The action for the container to take
* @throws FtpException
* @throws IOException
*/
@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 {
//todo servies...
return super.onDownloadStart(session, request);
}
@Override
public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
//todo servies...
return super.onDownloadEnd(session, request);
}
}

FtpConfig.java

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; /**
* 配置类
*/
@Configuration
public class FtpConfig extends CachingConfigurerSupport { @Value("${ftp.port}")
private Integer ftpPort; @Value("${ftp.passivePorts}")
private String ftpPassivePorts; @Value("${ftp.passiveExternalAddress}")
private String ftpPassiveExternalAddress; @Bean
public FtpServer createFtpServer(){
FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFactory factory = new ListenerFactory();/**
* 被动模式
*/
DataConnectionConfigurationFactory dataConnectionConfigurationFactory=new DataConnectionConfigurationFactory();
dataConnectionConfigurationFactory.setIdleTime(60);
dataConnectionConfigurationFactory.setActiveLocalPort(ftpPort);
dataConnectionConfigurationFactory.setPassiveIpCheck(true);
dataConnectionConfigurationFactory.setPassivePorts(ftpPassivePorts);
dataConnectionConfigurationFactory.setPassiveExternalAddress(ftpPassiveExternalAddress);
factory.setDataConnectionConfiguration(dataConnectionConfigurationFactory.createDataConnectionConfiguration());
// replace the default listener
serverFactory.addListener("default", factory.createListener()); PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
try
{
ClassPathResource classPathResource = new ClassPathResource("users.properties");
userManagerFactory.setUrl(classPathResource.getURL());
}
catch (Exception e){
throw new RuntimeException("配置文件users.properties不存在");
} userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
serverFactory.setUserManager(userManagerFactory.createUserManager()); Map<String, Ftplet> m = new HashMap<String, Ftplet>();
m.put("miaFtplet", new UploadListener()); serverFactory.setFtplets(m);
// start the server
FtpServer server = serverFactory.createServer(); return server;
}
}

InitFtpServer.java

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.CommandLineRunner;
import org.springframework.stereotype.Component; /**
* springboot启动时初始化ftpserver
*/
@Component
public class InitFtpServer implements CommandLineRunner { public static final Logger log = LoggerFactory.getLogger(FtpServer.class); @Autowired
private FtpServer server; @Override
public void run(String... args) throws Exception {
try {
server.start();
log.info(">>>>>>>ftp start success ");
} catch (FtpException e) {
e.printStackTrace();
log.error(">>>>>>>ftp start error {}", e); }
}
}

在resource下创建users.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. #表示admin的密码是123456 以下都是admin的参数设置,可以多个
ftpserver.user.admin.userpassword=123456
ftpserver.user.admin.homedirectory=/home/data/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

yml配置文件加入

ftp:
port: 21 #ftp连接端口
passivePorts: 20 #被动连接数据传输端口
passiveExternalAddress: 127.0.0.1 #部署的服务器ip地址

然后启动SpringBoot服务

工具连接

账号密码就是配置文件里面的密码

SpringBoot内嵌ftp服务的更多相关文章

  1. apache ftp server的简单入门(java应用内嵌ftp server)

    Apache Ftp Server:(强调) Apache Ftp Server 是100%纯Java的FTP服务器软件,它采用MINA网络框架开发具有非常好的性能.Apache FtpServer ...

  2. 查看和指定SpringBoot内嵌Tomcat的版本

    查看当前使用的Tomcat版本号 Maven Repository中查看 比如我们需要查Spring Boot 2.1.4-RELEASE的内嵌Tomcat版本, 可以打开链接: https://mv ...

  3. SpringBoot内嵌Tomcat开启APR模式(运行环境为Centos7)

    网上查到的一些springboot内嵌的tomcat开启apr的文章,好像使用的springboot版本较老,在SpringBoot 2.0.4.RELEASE中已经行不通了.自己整理了一下,供参考. ...

  4. spring-boot内嵌三大容器https设置

    spring-boot内嵌三大容器https设置 spring-boot默认的内嵌容器为tomcat,除了tomcat之前还可以设置jetty和undertow. 1.设置https spring-b ...

  5. springboot~内嵌redis的使用

    对于单元测试来说,我们应该让它尽量保持单一环境,不要与网络资源相通讯,这样可以保证测试的稳定性与客观性,对于springboot这个框架来说,它集成了单元测试JUNIT,同时在设计项目时,你可以使用多 ...

  6. WPF内嵌WCF服务对外提供接口

    要测试本帖子代码请记得管理员权限运行vs. 我写这个帖子的初衷是在我做surface小车的时候有类似的需求,感觉这个功能还挺有意思的,所以就分享给大家,网上有很多关于wcf的文章 我就不一一列举了.公 ...

  7. SpringBoot内嵌数据库的使用(H2)

    配置数据源(DataSource) Java的javax.sql.DataSource接口提供了一个标准的使用数据库连接的方法. 传统做法是, 一个DataSource使用一个URL以及相应的证书去构 ...

  8. 012.Delphi插件之QPlugins,多实例内嵌窗口服务

    这个DEMO中主要是在DLL中建立了一个IDockableControl类,并在DLL的子类中写了具体的实现方法. 在主程序exe中,找到这个服务,然后调用DLL的内嵌方法,把DLL插件窗口内嵌到主程 ...

  9. SpringBoot 内嵌容器的比较

    Spring Boot内嵌容器支持Tomcat.Jetty.Undertow.为什么选择Undertow? 这里有一篇文章,时间 2017年1月26日发布的: 参考 Tomcat vs. Jetty ...

随机推荐

  1. vue使用axios读取本地json文件来显示echarts折线图

    编辑器:HBuilderx axios文档:http://www.axios-js.com/zh-cn/docs/ echarts实例:https://echarts.apache.org/examp ...

  2. 贪心/构造/DP 杂题选做

    本博客将会收录一些贪心/构造的我认为较有价值的题目,这样可以有效的避免日后碰到 P7115 或者 P7915 这样的题就束手无策进而垫底的情况/dk 某些题目虽然跟贪心关系不大,但是在 CF 上有个 ...

  3. AtCoder Grand Contest 055 题解

    A 赛时直到最后 10min 才做出这个 A 题,之前猜了一个结论一直没敢写,本来不抱啥希望 AC 的结果比赛结束时交了一发竟然 A 了,由此可见我的水平之菜/dk 考虑每次取出字符串开头字符,不妨设 ...

  4. AT3945 [ARC092D] Two Faced Edges

    要求,翻转一条边,强连通分量个数是否会改变. 考虑连通分量个数会改变的因素: 即\(v\to u\)是否成立,以及翻转前,是否有一条\(u \to v\)的路径不经过该条边 以上当只有一个满足时,连通 ...

  5. 毕业设计之zabbix---自带模板监控mysql内容

    自带模板是不能直接建立连接就可以用的 必须经历一下几步: 建立用户权限: [root@mysql.quan.bbs lib]$mysql -u root -p Enter password: Welc ...

  6. zabbix-磁盘状态脚本

    #/bin/sh Device=$1 DISK=$2 case $DISK in tps) iostat -dmt 1 2|grep "\b$Device\b"|tail -1|a ...

  7. urllib的基本使用介绍

    1. urllib中urlopen的基本使用介绍 1 ### urllib中urlopen的基本使用介绍 2 3 ## urlopen的基本用法(GET请求) 4 import urllib.requ ...

  8. 标准非STL容器 : bitset

    1. 概念 什么是"标准非STL容器"?标准非STL容器是指"可以认为它们是容器,但是他们并不满足STL容器的所有要求".前文提到的容器适配器stack.que ...

  9. MySQL插入大量数据探讨

    笔者想进行数据库查询优化探索,但是前提是需要一个很大的表,因此得先导入大量数据至一张表中. 准备工作 准备一张表,id为主键且自增: 方案一 首先我想到的方案就是通过for循环插入 xml文件: &l ...

  10. C/C++ Qt 数据库QSql增删改查组件应用

    Qt SQL模块是Qt中用来操作数据库的类,该类封装了各种SQL数据库接口,可以很方便的链接并使用,数据的获取也使用了典型的Model/View结构,通过MV结构映射我们可以实现数据与通用组件的灵活绑 ...