Linux安裝ftp、Java的FTP上傳下載文件工具類
- 文章說明
- Linux安裝vsftpd
- Java的工具類
文章說明
網上找到說linux安裝ftp,采用vsftpd,在后續的配置中少了一些說明,給我折磨了許久,寫下這篇文章來記錄
Linux安裝vsftpd
命令非常簡單,而且不需要什么額外的配置
yum install vsftpd -y
service vsftpd start
service vsftpd statusvi /etc/vsftpd/vsftpd.conf # 添加下面三行pasv_enable=YES # 允許被動模式
pasv_min_port=5000 # 被動模式下服務器使用的最小端口
pasv_max_port=5000 # 被動模式下服務器使用的最大端口
上面的幾個命令就是安裝配置vsftpd的步驟,當然如果開啟了防火墻則需要開啟那個端口,然后如果是云服務器也需要在規則里面放開這個端口的通行
改為bash腳本如下
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用戶,采用如下命令
useradd jack
passwd jack
并且為它設置一下密碼,然后就可以了,需要注意的是有一些用戶默認是不可以采用ftp連接的,在 /etc/vsftpd/user_list 里面,采用如下命令可以查看到內容
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);}}}