生活随笔
收集整理的這篇文章主要介紹了
通过FTP4J 实现 FTPS 连接
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?FTPS:
一種多傳輸協(xié)議,相當(dāng)于加密版的FTP。當(dāng)你在FTP服務(wù)器上收發(fā)文件的時(shí)候,你面臨兩個(gè)風(fēng)險(xiǎn)。第一個(gè)風(fēng)險(xiǎn)是在上載文件的時(shí)候?yàn)槲募用堋5诙€(gè)風(fēng)險(xiǎn)是,這些文件在你等待接收方下載的時(shí)候?qū)⑼A粼贔TP服務(wù)器上,這時(shí)你如何保證這些文件的安全。你的第二個(gè)選擇(創(chuàng)建一個(gè)支持SSL的FTP服務(wù)器)能夠讓你的主機(jī)使用一個(gè)FTPS連接上載這些文件。這包括使用一個(gè)在FTP協(xié)議下面的SSL層加密控制和數(shù)據(jù)通道。一種替代FTPS的協(xié)議是安全文件傳輸協(xié)議(SFTP) 。這個(gè)協(xié)議使用SSH文件傳輸協(xié)議加密從客戶機(jī)到服務(wù)器的FTP連接。 FTPS是在安全套接層使用標(biāo)準(zhǔn)的FTP協(xié)議和指令的一種增強(qiáng)型FTP協(xié)議,為FTP協(xié)議和數(shù)據(jù)通道增加了SSL安全功能。FTPS也稱作“FTP-SSL”和“FTP-over-SSL”。SSL是一個(gè)在客戶機(jī)和具有SSL功能的服務(wù)器之間的安全連接中對(duì)數(shù)據(jù)進(jìn)行加密和解密的協(xié)議。
?
當(dāng)使用FTPS與服務(wù)器連接時(shí),有兩種方法:顯式和隱式。 簡(jiǎn)單來(lái)說(shuō):顯示又叫FTPES , FTPS客戶端跟FTPS服務(wù)器必須顯式使用一種同樣的加密方法。如果客戶端不要求加密,服務(wù)器也允許非加密通訊。 隱式 就是客戶端直接通過(guò)TSL/SSL加密與服務(wù)器聯(lián)系,如果服務(wù)器無(wú)響應(yīng),則停止通訊。
?
FTP4J 支持 FTPS/FTPES secured connection,其中使用FTPES還是原來(lái)的21端口,使用FTPS使用的是990端口,使用SFTP的是22端口 ,以下說(shuō)的不包含SFTP內(nèi)容。
可以查看Serv-U域詳細(xì)信息查看服務(wù)邦定的端口,默認(rèn)情況下是以下內(nèi)容:
?
如果我們使用flashfxp進(jìn)行連接,則使用不同連接方式時(shí)要進(jìn)行選擇,普通FTP連接使用21端口 ,不用選擇:
?
我們通過(guò)21端口進(jìn)行顯示FTPS連接 :
Java代碼
package test;import it.sauronsoftware.ftp4j.FTPClient;import java.security.SecureRandom;import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSocketFactory;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;public class Ftp4jTest {public static void main(String[] args) {try {TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() {return null ;} public void checkClientTrusted(X509Certificate[] certs,String authType) { } public void checkServerTrusted(X509Certificate[] certs,String authType) { } } }; SSLContext sslContext = null ; sslContext = SSLContext.getInstance("SSL" ); sslContext.init(null , trustManager, new SecureRandom()); SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); FTPClient client = new FTPClient(); client.setSSLSocketFactory(sslSocketFactory); client.setSecurity(FTPClient.SECURITY_FTPES); client.connect("192.168.1.122" , 21 ); client.login("123" , "123123" ); System.out.println(client.toString()); System.out.println(client.currentDirectory()); } catch (Exception e) { e.printStackTrace(); } } }
package test;
import it.sauronsoftware.ftp4j.FTPClient;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/*** 通過(guò)21端口進(jìn)行顯示FTPS連接* @說(shuō)明 * @author cuisuqiang* @version 1.0* @since*/
public class Ftp4jTest {public static void main(String[] args) {try {TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return null;}public void checkClientTrusted(X509Certificate[] certs,String authType) {}public void checkServerTrusted(X509Certificate[] certs,String authType) {}} };SSLContext sslContext = null;sslContext = SSLContext.getInstance("SSL");sslContext.init(null, trustManager, new SecureRandom());SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();FTPClient client = new FTPClient();client.setSSLSocketFactory(sslSocketFactory);client.setSecurity(FTPClient.SECURITY_FTPES); client.connect("192.168.1.122", 21);client.login("123", "123123");System.out.println(client.toString());System.out.println(client.currentDirectory());} catch (Exception e) {e.printStackTrace();}}
}
?
代碼會(huì)打印連接信息和當(dāng)前目錄
使用990端口進(jìn)行隱式FTPS連接 :
Java代碼
package test;import it.sauronsoftware.ftp4j.FTPClient;import java.security.SecureRandom;import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSocketFactory;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;public class Ftp4jTest {public static void main(String[] args) {try {TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() {return null ;} public void checkClientTrusted(X509Certificate[] certs,String authType) { } public void checkServerTrusted(X509Certificate[] certs,String authType) { } } }; SSLContext sslContext = null ; sslContext = SSLContext.getInstance("SSL" ); sslContext.init(null , trustManager, new SecureRandom()); SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); FTPClient client = new FTPClient(); client.setSSLSocketFactory(sslSocketFactory); client.setSecurity(FTPClient.SECURITY_FTPS); client.connect("192.168.1.122" , 990 ); client.login("123" , "123123" ); System.out.println(client.toString()); System.out.println(client.currentDirectory()); } catch (Exception e) { e.printStackTrace(); } } }
package test;
import it.sauronsoftware.ftp4j.FTPClient;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/*** 進(jìn)行隱式FTPS連接* @說(shuō)明 * @author cuisuqiang* @version 1.0* @since*/
public class Ftp4jTest {public static void main(String[] args) {try {TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return null;}public void checkClientTrusted(X509Certificate[] certs,String authType) {}public void checkServerTrusted(X509Certificate[] certs,String authType) {}} };SSLContext sslContext = null;sslContext = SSLContext.getInstance("SSL");sslContext.init(null, trustManager, new SecureRandom());SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();FTPClient client = new FTPClient();client.setSSLSocketFactory(sslSocketFactory);client.setSecurity(FTPClient.SECURITY_FTPS);client.connect("192.168.1.122", 990);client.login("123", "123123");System.out.println(client.toString());System.out.println(client.currentDirectory());} catch (Exception e) {e.printStackTrace();}}
}
?
打印內(nèi)容相同
?
進(jìn)行顯示還是隱式連接的最大不同是指定了連接方式:
這個(gè)情況官方也給出了詳細(xì)的說(shuō)明:
The ftp4j library supports both FTPS (FTP over implicit TLS/SSL) and FTPES (FTP over explicit TLS/SSL). The setSecurity() method can be used to turn on the feature: client.setSecurity(FTPClient.SECURITY_FTPS); // enables FTPS client.setSecurity(FTPClient.SECURITY_FTPES); // enables FTPES Both methods must be called before connecting the remote server. If the security is set to SECURITY_FTPS, the default port used by the connect() method changes to 990
?
請(qǐng)您到ITEYE網(wǎng)站看原創(chuàng),謝謝!
http://cuisuqiang.iteye.com/ !?
轉(zhuǎn)載于:https://blog.51cto.com/ajiao13/1132563
總結(jié)
以上是生活随笔 為你收集整理的通过FTP4J 实现 FTPS 连接 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。