【FTCPSClient 连接 FTP服务器,服务器开启SSL认证】

FTCPSClient 连接 SVFTPD服务器,服务器开启SSL认证

很多文章都介绍了FTPClient如何连接ftp服务器,但却很少有人说如何连接一台开了SSL认证的ftp服务器,现在代码来了。

##依赖

		<!-- ftp远程工具 -->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
		<dependency>
			<groupId>com.jcraft</groupId>
			<artifactId>jsch</artifactId>
			<version>0.1.53</version>
		</dependency>
		<!-- pool 对象池 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
		</dependency>

代码



import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPSClient;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStore;

public class FTPSExample {
    public static void main(String[] args) {
        String server = "ftp.example.com";
        int port = 21;
        String username = "your-username";
        String password = "your-password";
        FTPSClient ftpsClient = new FTPSClient("TLS");
        try {
            // 设置SSL连接相关配置
            SSLContext sslContext = SSLContext.getInstance("TLS");
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream keyStoreFile = new FileInputStream("path-to-key-store-file");
            keyStore.load(keyStoreFile, "key-store-password".toCharArray());
            keyStoreFile.close();
            keyManagerFactory.init(keyStore, "key-store-password".toCharArray());
            TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
            sslContext.init(keyManagerFactory.getKeyManagers(), trustManagers, null);
            ftpsClient.setContext(sslContext);
            // 连接到FTP服务器
            ftpsClient.connect(server, port);
            ftpsClient.login(username, password);
            // 设置文件传输模式为二进制
            ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
            // 上传文件
            File localFile = new File("local-file.txt");
            String remoteFile = "remote-file.txt";
            FileInputStream inputStream = new FileInputStream(localFile);
            ftpsClient.storeFile(remoteFile, inputStream);
            inputStream.close();
            System.out.println("文件上传成功");
            // 下载文件
            String downloadFile = "remote-file.txt";
            FileOutputStream outputStream = new FileOutputStream("downloaded-file.txt");
            ftpsClient.retrieveFile(downloadFile, outputStream);
            outputStream.close();
            System.out.println("文件下载成功");
            // 断开连接
            ftpsClient.logout();
            ftpsClient.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

写在最后,vsftpd 配置开启ssl加密不再这里说明,可参考其它文章。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值