ApacheFTPServer快速搭建FTP服务由浅入深(一)

如何用org.apache.ftpserver搭建FTP服务

1、引入jar包

我用的是1.1.1的版本,最后更新时间是2017年4月。官方文档链接: http://mina.apache.org/ftpserver-project/index.html

		<!-- 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>
        <!-- https://mvnrepository.com/artifact/org.apache.ftpserver/ftplet-api -->
        <dependency>
            <groupId>org.apache.ftpserver</groupId>
            <artifactId>ftplet-api</artifactId>
            <version>1.1.1</version>
        </dependency>
2、在main方法中运行FTP服务

首先创建一个文件,例如E:\users.properties用于保存我们的用户信息。

package apache.ftp.server.test;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.usermanager.PasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FtpServerApplication {
    public static void main(String[] args) throws Exception {
        FtpServerFactory serverFactory = new FtpServerFactory();

        //choose any. We're telling the FTP-server where to read it's user list
        PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
        //本地用于保存用户信息的文件,运行前创建准备好
        userManagerFactory.setFile(new File("E:\\users.properties"));
        userManagerFactory.setPasswordEncryptor(new PasswordEncryptor() {
            //We store clear-text passwords in this example
            @Override
            public String encrypt(String password) {
            	//不加密
                return password;
            }

            @Override
            public boolean matches(String passwordToCheck, String storedPassword) {
            	//直接比对
                return passwordToCheck.equals(storedPassword);
            }
        });

        UserManager um = userManagerFactory.createUserManager();

        //The user to save
        BaseUser user = new BaseUser();
        //设置用户名
        user.setName("admin");
        //设置密码
        user.setPassword("admin");
        //设置已存在的目录为该用户的主目录
        user.setHomeDirectory("E:\\FTP");
        //用户权限信息
        List<Authority> authorities = new ArrayList<Authority>();
        //添加写的权限
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);

        //Save the user to the user list on the filesystem
        //保存用户
        um.save(user);
        serverFactory.setUserManager(um);
		//创建服务
        FtpServer server = serverFactory.createServer();

        //Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set
        //最后一步
        server.start();
    }
}
3、测试

在win系统环境下用FTP命令测试,win+r快捷键打开运行窗口,输入cmd,进入页面。默认地址为: ftp://127.0.0.1/ 默认端口号:21(也可以用FTP客户端测试,常用的客户端有FileZilla、FlashFXP、Xftp等)

PS C:\Users\admin\Desktop> ftp
ftp> open 127.0.0.1
连接到 127.0.0.1。
220 Service ready for new user.
530 Access denied.
用户(127.0.0.1:(none)): admin
331 User name okay, need password for admin.
密码:
230 User logged in, proceed.
ftp> ls
200 Command PORT okay.
150 File status okay; about to open data connection.
test1
test2
test3
test4
226 Closing data connection.
ftp: 收到 31 字节,用时 0.01秒 6.20千字节/秒。
ftp>

看到目录清单,即搭建成功。
此时我们打开我们创建的用户保存用户信息的文件E:\users.properties。可以看到我们设置的用户信息

#Generated file - don't edit (please)
#Mon Mar 23 22:39:42 CST 2020
ftpserver.user.admin.userpassword=admin
ftpserver.user.test.writepermission=true
ftpserver.user.test.idletime=0
ftpserver.user.admin.idletime=0
ftpserver.user.test.enableflag=true
ftpserver.user.admin.writepermission=true
ftpserver.user.test.userpassword=test
ftpserver.user.admin.enableflag=true
ftpserver.user.admin.homedirectory=E\:\\FTP
ftpserver.user.test.homedirectory=d\:/home/

你可能感兴趣的:(JAVA,java,apache,ftp,spring,boot)