Linux安装ftp、Java的FTP上传下载文件工具类

Linux安装ftp、Java的FTP上传下载文件工具类

文章说明

网上找到说linux安装ftp,采用vsftpd,在后续的配置中少了一些说明,给我折磨了许久,写下这篇文章来记录

Linux安装vsftpd

命令非常简单,而且不需要什么额外的配置

powershell 复制代码
yum install vsftpd -y
service vsftpd start
service vsftpd status

vi /etc/vsftpd/vsftpd.conf # 添加下面三行

pasv_enable=YES			# 允许被动模式
pasv_min_port=5000     # 被动模式下服务器使用的最小端口
pasv_max_port=5000     # 被动模式下服务器使用的最大端口

上面的几个命令就是安装配置vsftpd的步骤,当然如果开启了防火墙则需要开启那个端口,然后如果是云服务器也需要在规则里面放开这个端口的通行
改为bash脚本如下

powershell 复制代码
yum install vsftpd -y
echo -e "pasv_enable=YES\npasv_min_port=5000\npasv_max_port=5000" | sudo tee -a /etc/vsftpd/vsftpd.conf
service vsftpd start
service vsftpd status

在安装并开启好vsftpd服务后,还需要创建用户,如jack用户,采用如下命令

powershell 复制代码
useradd jack
passwd jack

并且为它设置一下密码,然后就可以了,需要注意的是有一些用户默认是不可以采用ftp连接的,在 /etc/vsftpd/user_list 里面,采用如下命令可以查看到内容

powershell 复制代码
nl /etc/vsftpd/user_list
powershell 复制代码
1	# vsftpd userlist
2	# If userlist_deny=NO, only allow users in this file
3	# If userlist_deny=YES (default), never allow users in this file, and
4	# do not even prompt for a password.
5	# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
6	# for users that are denied.
7	root
8	bin
9	daemon
10	adm
11	lp
12	sync
13	shutdown
14	halt
15	mail
16	news
17	uucp
18	operator
19	games
20	nobody

Java的工具类

简单引入依赖

xml 复制代码
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.8.0</version>
</dependency>

工具类代码

java 复制代码
package com.boot.util;

import org.apache.commons.net.ftp.FTPClient;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FtpUtil {
    private final String host;
    private final Integer port;
    private final String username;
    private final String password;
    private final String path;

    public FtpUtil(String host, Integer port, String username, String password, String path) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.path = path;
    }

    private FTPClient getFtpClient() throws IOException {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(host, port);
        ftpClient.login(username, password);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory(path);
        return ftpClient;
    }

    public void uploadFile(String fileName, InputStream inputStream) throws Exception {
        FTPClient ftpClient = getFtpClient();
        ftpClient.storeFile(new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1), inputStream);
        ftpClient.disconnect();
    }

    public void downloadFile(String fileName, OutputStream outputStream) throws Exception {
        FTPClient ftpClient = getFtpClient();
        try (InputStream inputStream = ftpClient.retrieveFileStream(new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1))) {
            byte[] buf = new byte[1024];
            int read;
            while ((read = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, read);
            }
            ftpClient.disconnect();
        }
    }

    public static void main(String[] args) throws Exception {
        FtpUtil ftpUtil = new FtpUtil("****", 21, "****", "****", "****");
        ftpUtil.uploadFile("测试文件.txt", Files.newInputStream(Paths.get("C:/file/测试文件.txt")));

        try (FileOutputStream outputStream = new FileOutputStream("C:/file/测试文件2.txt")) {
            ftpUtil.downloadFile("测试文件.txt", outputStream);
        }
    }

}
相关推荐
秋风起,再归来~7 分钟前
【Linux庖丁解牛】—进程优先级!
linux·运维·服务器
cosX+sinY26 分钟前
ubuntu 20.04 编译运行lio-sam,并保存为pcd
linux·ubuntu·机器人
24k小善1 小时前
Flink TaskManager详解
java·大数据·flink·云计算
想不明白的过度思考者1 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
Lary_Rock1 小时前
Android 编译问题 prebuilts/clang/host/linux-x86
android·linux·运维
熬夜学编程的小王1 小时前
【Linux篇】理解信号:如何通过信号让程序听从操作系统的指令
linux·信号产生·软件条件产生信号
.生产的驴1 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
绵绵细雨中的乡音2 小时前
Linux进程学习【基本认知】
linux·运维·学习
猿周LV2 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
晨集2 小时前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同