ftps = ftp + ssl
sftp = ssh + scp,这个不适合大模式的文件传输
领导表达有误,实际上是希望把ftp改为ftps
一、FTPS服务器配置
- 先确认是否已经安装了vsftpd。没有的话则安装一下
yum install vsftpd
systemctl enable vsftpd
systemctl restart vsftpd
- 创建私钥、证书
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/vsftpd/vsftpd_ssl_key.pem -out /etc/vsftpd/vsftpd_ssl_cert.pem
- 添加配置到vsftpd.conf文件中
ssl_enable=YES
ssl_tlsv1=YES
ssl_sslv2=YES
ssl_sslv3=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_ciphers=HIGH:AES256-SHA:!aNULL
rsa_cert_file=/etc/vsftpd/vsftpd_ssl_cert.pem
rsa_private_key_file=/etc/vsftpd/vsftpd_ssl_key.pem
require_ssl_reuse=NO
- 重启ftp服务
systemctl restart vsftpd
二、FTPS工具类
- 加入依赖
<dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.11.0</version></dependency>
- 工具类
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPSClient;import java.io.File;
import java.io.FileInputStream;public class FtpsUtil {/*** 打开FTP连接** @param hostname hostname* @param port port* @param username username* @param password password*/public static FTPSClient openFTP(String hostname, int port, String username, String password) {// 显示方式传false,隐式方式传trueFTPSClient ftpsClient = new FTPSClient(false);ftpsClient.setControlEncoding("UTF-8");try {ftpsClient.connect(hostname, port);boolean flag = ftpsClient.login(username, password);// 切换到被动模式ftpsClient.setControlEncoding("UTF-8");ftpsClient.setFileType(FTPClient.BINARY_FILE_TYPE);ftpsClient.enterLocalPassiveMode();ftpsClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);ftpsClient.execPROT("P");ftpsClient.setAuthValue("TLS");if (!flag) {System.out.println("login failed");return null;}System.out.println("ftpsClient connect");String path = ftpsClient.printWorkingDirectory();System.out.println("path:" + path);} catch (Exception e) {e.printStackTrace();}return ftpsClient;}/*** 关闭FTP连接** @param ftpsClient ftp客户端*/public static void closeFTP(FTPSClient ftpsClient) {try {ftpsClient.logout();ftpsClient.disconnect();} catch (Exception e) {e.printStackTrace();}}/*** 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建** @param remote 目录* @param ftpsClient ftp客户端*/public static boolean createDirectory(String remote, FTPSClient ftpsClient) {String directory = remote;try {if (!remote.endsWith("/")) {directory = directory + "/";}// 如果远程目录不存在,则递归创建远程服务器目录if (!directory.equals("/") && !ftpsClient.changeWorkingDirectory(directory)) {int start = 0;int end = 0;if (directory.startsWith("/")) {start = 1;} else {start = 0;}end = directory.indexOf("/", start);String paths = "", path = "";while (true) {String subDirectory = remote.substring(start, end);path = path + "/" + subDirectory;// 目录不存在就创建if (!ftpsClient.changeWorkingDirectory(subDirectory)) {if (ftpsClient.makeDirectory(subDirectory)) {ftpsClient.changeWorkingDirectory(subDirectory);}}paths = paths + "/" + subDirectory;start = end + 1;end = directory.indexOf("/", start);// 检查所有目录是否创建完毕if (end <= start) {break;}}}return true;} catch (Exception e) {e.printStackTrace();}return false;}/*** 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建** @param filePath 上传文件本地路径* @param ftpPath 上传文件FTP路径* @param ftpsClient ftp客户端*/public static void uploadFileToFTP(String filePath, String ftpPath, FTPSClient ftpsClient) {try {File file = new File(filePath);boolean changeFlag = ftpsClient.changeWorkingDirectory(ftpPath);System.out.println("changeFlag:" + changeFlag);if (!changeFlag) {boolean createFlag = createDirectory(ftpPath, ftpsClient);System.out.println("createFlag:" + createFlag);}String uploadPath = ftpsClient.printWorkingDirectory();System.out.println("uploadPath:" + uploadPath);ftpsClient.setFileType(FTPClient.BINARY_FILE_TYPE);boolean uploadFlag = ftpsClient.storeUniqueFile(file.getName(), new FileInputStream(file));System.out.println("uploadFlag:" + uploadFlag);System.out.println("uploadFlag:" + ftpsClient.getReplyString());} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {// 打开ftp连接FTPSClient ftpsClient = openFTP("192.168.0.1", 21, "ftpuser", "97joiuewr@!s");if (null == ftpsClient) {return;}// 上传文件uploadFileToFTP("D:/test.txt", "/home/ftpuser", ftpsClient);// 关闭ftp连接closeFTP(ftpsClient);}}