利用ftp4j实现ftps加密上传

//--------------------这是是main方法---------

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

 

public class ss {

 /**
  * @param args  

  * @throws FTPException
  * @throws FTPIllegalReplyException
  * @throws IOException
  * @throws IllegalStateException
  * @throws FTPListParseException
  */
 public static void main(String[] args) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPListParseException {
  
    X509TrustManager[] trustAll = new X509TrustManager[] { new X509TrustManager() {
     public java.security.cert.X509Certificate[] getAcceptedIssuers() {
      return null;
     }

     public void checkClientTrusted(
       java.security.cert.X509Certificate[] certs, String authType) {
     }

     public void checkServerTrusted(
       java.security.cert.X509Certificate[] certs, String authType) {
     }
    }
    };
  
  SSLContext sslContext = null;
  FTPClient client = new FTPClient();
  try {
   sslContext = SSLContext.getInstance("SSL");
   sslContext.init(null, trustAll,  new java.security.SecureRandom());
   SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
   client.setSSLSocketFactory(sslSocketFactory);
   client.setSecurity(FTPClient.SECURITY_FTPS);
  } catch (KeyManagementException e) {
   e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  }
   
  try {
   File pf = new File("ftps.properties");
         InputStream pi = new FileInputStream(pf);
   Properties p = new Properties();
         p.load(pi);
   //服务器IP地址
   String ip=p.getProperty("serverIp");
   //服务器端口
   String port=p.getProperty("serverPort");
   //服务器用户名
   String userName=p.getProperty("userName");
   //服务器密码
   String passWord=p.getProperty("passWord");
   //本地文件
   String localFile=p.getProperty("localFile");
   System.out.println("----开始连接接服务器------");
   System.out.println("IP> "+ip+" 端口> "+port+" 用户> "+userName);
   
   client.connect(ip, Integer.valueOf(port));
   client.login(userName, passWord);
   
   
   String [] str=localFile.split(",");
   System.out.println(" 总共 "+str.length+"文件要上传");
   for(int i=0;i<str.length;i++){
    
    System.out.println("  ");
    System.out.println("  第"+i+"文件");
    System.out.println("---开始上传 "+str[i]+" ---");
    client.upload(new java.io.File(str[i]));
    System.out.println("---上传结束---");
    System.out.println("  ");
   }
  } catch (FTPDataTransferException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }

 }
 
}
   


//-------------------------配置文件(ftps.properties)-----------------------

# ftps 上传配置文件

#服务器IP地址
serverIp=192.168.1.17
#服务器端口
serverPort=991
#服务器用户名
userName=mjg
#服务器密码
passWord=123

#本地文件 多个文件用逗号分开
localFile=D\:output.log,D\:output2.log

 

你可能感兴趣的:(java,.net,Security)