通过JSCH 实现FTP各种操作



FTPS:

一种多传输协议,相当于加密版的FTP。当你在FTP服务器上收发文件的时候,你面临两个风险。第一个风险是在上载文件的时候为文件加密。第二个风险是,这些文件在你等待接收方下载的时候将停留在FTP服务器上,这时你如何保证这些文件的安全。你的第二个选择(创建一个支持SSL的FTP服务器)能够让你的主机使用一个FTPS连接上载这些文件。这包括使用一个在FTP协议下面的SSL层加密控制和数据通道。一种替代FTPS的协议是安全文件传输协议(SFTP)。这个协议使用SSH文件传输协议加密从客户机到服务器的FTP连接。
FTPS是在安全套接层使用标准的FTP协议和指令的一种增强型FTP协议,为FTP协议和数据通道增加了SSL安全功能。FTPS也称作“FTP-SSL”和“FTP-over-SSL”。SSL是一个在客户机和具有SSL功能的服务器之间的安全连接中对数据进行加密和解密的协议。



当使用FTPS与服务器连接时,有两种方法:显式和隐式。
简单来说:
显示又叫FTPES, FTPS客户端跟FTPS服务器必须显式使用一种同样的加密方法。如果客户端不要求加密,服务器也允许非加密通讯。
隐式 就是客户端直接通过TSL/SSL加密与服务器联系,如果服务器无响应,则停止通讯。



FTP4J 支持 FTPS/FTPES secured connection,其中使用FTPES还是原来的21端口,使用FTPS使用的是990端口,使用SFTP的是22端口,以下说的不包含SFTP内容。

可以查看Serv-U域详细信息查看服务邦定的端口,默认情况下是以下内容:



如果我们使用flashfxp进行连接,则使用不同连接方式时要进行选择,普通FTP连接使用21端口,不用选择:



我们通过21端口进行显示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; 
    /**
     * 通过21端口进行显示FTPS连接
     * @说明 
     * @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(); 
            } 
        } 
    } 



代码会打印连接信息和当前目录

使用990端口进行隐式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; 
    /**
     * 进行隐式FTPS连接
     * @说明 
     * @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(); 
            } 
        } 
    } 



打印内容相同



进行显示还是隐式连接的最大不同是指定了连接方式:

这个情况官方也给出了详细的说明:

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



请您到ITEYE网站看原创,谢谢!

http://cuisuqiang.iteye.com/ !

你可能感兴趣的:(java,ftp,ftp4j,FTPS)