Apache ftpserver整合springboot-阿里云开发者社区

开发者社区> 开发与运维> 正文

Apache ftpserver整合springboot

简介: Springboot集成ftpserver,添加用户

Apache ftpserver集成


Apache ftpserver由Java语言开发的Ftp服务器,集成到其它程序,正常运行需要:

  • mina-core, 2.0-M3 or later
  • slf4j-api
  • A SLF4J implementation of your choice, for example slf4j-simple-1.5.3.jar
  • ftplet-api
  • ftpserver-core
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25 </version>
</dependency>
<dependency>
    <groupId>org.apache.ftpserver</groupId>
    <artifactId>ftpserver-core</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.ftpserver</groupId>
    <artifactId>ftplet-api</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.mina</groupId>
    <artifactId>mina-core</artifactId>
    <version>2.0.16</version>
</dependency>

Springboot应用集成 Apache ftpserver


_1

代码


@Configuration
public class FtpConfig {
    private final Logger logger = LoggerFactory.getLogger(FtpConfig.class);

    @Bean
    public UserManager getUserManger() throws FtpException {

        FtpServerFactory serverFactory = new FtpServerFactory();
        
        // ftp服务监听的端口号
        ListenerFactory factory = new ListenerFactory();
        factory.setPort(2221);
        serverFactory.addListener("default", factory.createListener());
        
        // 创建用户
        BaseUser user = new BaseUser();
        user.setName("admin");
        user.setPassword("123456");
        user.setHomeDirectory("E:\\FTPServerPath");

        List<Authority> authorities = new ArrayList<Authority>();
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);
        UserManager userManager = serverFactory.getUserManager();
        userManager.save(user);
      
           serverFactory.setUserManager(userManager);
        
        FtpServer server = serverFactory.createServer();
        server.start();
        logger.info("ftp已经启动!");
 
        return userManager;
    }
}

@Component
public class SpringUtil implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        
        if(SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }
     
    //SpringUtils.getApplicationContext()获取applicationContext对象
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
     
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }
 
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }
 
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
 
}

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章