通过程序发布FTPS以及连接FTPS

本例中我们以Apache FTPServer为例进行发布FTPS, 也以Apache FTPClient为例进行客户端连接FTPS。

 

首先我们启动FTPServer

 

package examples.ftpServer; import java.io.File; import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.FtpServerFactory; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.listener.ListenerFactory; import org.apache.ftpserver.ssl.SslConfigurationFactory; import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; public class StartFTPS { /** * 通过程序启动FTP with SSL认证,以Apache FTPServer为例 * @param args * @throws FtpException */ public static void main(String[] args) throws FtpException { // TODO Auto-generated method stub FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFactory factory = new ListenerFactory(); // set the port of the listener factory.setPort(2221); // define SSL configuration SslConfigurationFactory ssl = new SslConfigurationFactory(); ssl.setKeystoreFile(new File("F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/kserver.keystore")); ssl.setKeystorePassword("123456"); // ssl.setTruststoreFile(new File("F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/tserver.keystore")); // ssl.setKeystorePassword("123456"); // set the SSL configuration for the listener factory.setSslConfiguration(ssl.createSslConfiguration()); factory.setImplicitSsl(true); // replace the default listener serverFactory.addListener("default", factory.createListener()); PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); userManagerFactory.setFile(new File("F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/conf/users.properties")); serverFactory.setUserManager(userManagerFactory.createUserManager()); // start the server FtpServer server = serverFactory.createServer(); server.start(); } }

 

 

然后是客户端连接FTPS

package examples.ftpClient; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.security.KeyStore; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTPSClient; public class ConnectFTPS { private static FTPSClient ftpsClient; private static final String trust_path = "F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/kclient.keystore"; private static final String trust_pw = "123456"; private static final String key_path = "F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/tclient.keystore"; private static final String key_pw = "123456"; private static final String serverIP = "127.0.0.1"; private static final int serverPort = 2221; private static final int defaultTimeout = 10000; private static final int soTimeout = 900000; private static final int dataTimeout = 5000; /** * 测试连接FTP With SSL,以Apache FTPServer为例 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { if (!connect("active")) { connect("passive"); } FileInputStream fs = new FileInputStream(trust_path); System.out.println("storeFile: " + ftpsClient.storeFile("test_file", fs)); fs.close(); ftpsClient.disconnect(); } /** * 登陆FTP * @param active * @return * @throws Exception */ private static boolean connect(String active) throws Exception { ftpsClient = new FTPSClient(true); ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); ftpsClient.setKeyManager(getKeyManager()); //ftpsClient.setTrustManager(getTrustManager()); ftpsClient.setDefaultTimeout(defaultTimeout); ftpsClient.connect(serverIP, serverPort); System.out.println("已经连接FTP"); ftpsClient.setSoTimeout(soTimeout); ftpsClient.getReplyCode(); ftpsClient.execPBSZ(0); ftpsClient.execPROT("P"); ftpsClient.login("admin", "admin"); ftpsClient.changeWorkingDirectory("/"); ftpsClient.setDataTimeout(dataTimeout); if (active.equalsIgnoreCase("active")) { ftpsClient.enterLocalActiveMode(); } else { ftpsClient.enterLocalPassiveMode(); } System.out.println("已经登陆FTP"); return testLink(); } /** * 遍历FTP文件 * @return */ private static boolean testLink() { long t1 = System.currentTimeMillis(); try { System.out.println("List file length:" + ftpsClient.listFiles().length); } catch (IOException e) { System.out.println(e.getMessage()); long t2 = System.currentTimeMillis(); long t = (t2 - t1) / 1000; System.out.println("t: " + t); try { ftpsClient.disconnect(); } catch (IOException e1) { e1.printStackTrace(); } return false; } return true; } private static KeyManager getKeyManager() throws Exception { KeyStore key_ks = KeyStore.getInstance("JKS"); key_ks.load(new FileInputStream(key_path), key_pw.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(key_ks, key_pw.toCharArray()); KeyManager[] km = kmf.getKeyManagers(); System.out.println("km len: " + km.length); return km[0]; } private static TrustManager getTrustManager() throws Exception { KeyStore trust_ks = KeyStore.getInstance("JKS"); trust_ks.load(new FileInputStream(trust_path), trust_pw.toCharArray()); TrustManagerFactory tf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tf.init(trust_ks); TrustManager[] tm = tf.getTrustManagers(); System.out.println("tm len: " + tm.length); return tm[0]; } }

 

以上实例是经过验证,如果对于其中有些概念性问题或者SSL证书的问题,可以参考先前的文章。

 

 

 

你可能感兴趣的:(Java开发)