linux ftp服务器vsftpd安装

发布时间 2023-12-09 15:22:48作者: 空虚公子

前提:一定关关闭selinux!!!,然后重启服务器

安装

  yum -y install vsftpd
  systemctl enable vsftpd.service
  systemctl start vsftpd.service

添加用户
  adduser ftptest
  passwd ftptest

配置文件

/etc/vsftpd/vsftpd.conf

anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
chown_username=ftpuser
xferlog_file=/var/log/xferlog
xferlog_std_format=YES
chroot_local_user=NO
chroot_list_enable=NO
local_root=/data/ftp
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
userlist_deny=NO
tcp_wrappers=YES
allow_writeable_chroot=YES
use_localtime=YES
pasv_enable=YES
pasv_min_port=30060
pasv_max_port=30090

 

java客户端上传文件

    public static void main(String[] args) {
        FTPClient ftpClient = new FTPClient();

        try (InputStream in = new FileInputStream(new File("d:/a.txt"))) {
            ftpClient.connect("192.168.88.131", 21);
            ftpClient.login("ftptest", "******");
            boolean isChange = ftpClient.changeWorkingDirectory("test");
            if (!isChange) {
                // 创建个人目录
                ftpClient.makeDirectory("test");
                // 切换
                ftpClient.changeWorkingDirectory("test");
            }

            // 文件类型
            ftpClient.setFileType(FTPClient.LOCAL_FILE_TYPE);
            boolean is = ftpClient.storeFile(System.currentTimeMillis() + ".txt", in);
            System.out.println(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }