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

文章说明

Linux安装vsftpd

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     # 被动模式下服务器使用的最大端口
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
useradd jack
passwd jack
nl /etc/vsftpd/user_list
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的工具类

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.8.0</version>
</dependency>
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);
        }
    }

}
07-07 15:59