zoukankan      html  css  js  c++  java
  • Ftp客户端需要TSL功能的文件上传

    Ftp客户端需要TSL功能

    1、由于最近做了一个项目,需要把打包的文件传输到对方的FTP服务器上,但是用普通的java连接ftp客户端总是连接不上去,对方却说ftp客户端需要开通TSL功能。

    直接上代码了!

      1 package cn.tendency.LaibinGaFtp;
      2 
      3 import it.sauronsoftware.ftp4j.FTPClient;
      4 
      5 import java.io.File;
      6 import java.security.SecureRandom;
      7 import java.security.cert.CertificateException;
      8 import java.security.cert.X509Certificate;
      9 
     10 import javax.net.ssl.SSLContext;
     11 import javax.net.ssl.SSLSocketFactory;
     12 import javax.net.ssl.TrustManager;
     13 import javax.net.ssl.X509TrustManager;
     14 
     15 import org.apache.log4j.Logger;
     16 
     17 import cn.tendency.utils.FilesUtil;
     18 
     19 public class Test2 {
     20     private static final Logger log = Logger.getLogger(Test2.class);
     21 
     22     public static void main(String[] args) {
     23     putFile("111.59.53.333", 21, "wewe", "ddfaf", "E:\houseTestData\zip", "E:\houseTestData\back");
     24     }
     25 
     26     public static void putFile(String host, int port, String username,
     27             String password, String zipPath, String backPath) {
     28         try {
     29             TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
     30 
     31                 public void checkClientTrusted(X509Certificate[] chain,
     32                         String authType) throws CertificateException {
     33                     // TODO Auto-generated method stub
     34 
     35                 }
     36 
     37                 public void checkServerTrusted(X509Certificate[] chain,
     38                         String authType) throws CertificateException {
     39                     // TODO Auto-generated method stub
     40 
     41                 }
     42 
     43                 public X509Certificate[] getAcceptedIssuers() {
     44                     // TODO Auto-generated method stub
     45                     return null;
     46                 }
     47 
     48             } };
     49             SSLContext sslContext = null;
     50             sslContext = SSLContext.getInstance("SSL");
     51             sslContext.init(null, trustManager, new SecureRandom());
     52             SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
     53             FTPClient client = new FTPClient();
     54             client.setSSLSocketFactory(sslSocketFactory);
     55             client.setSecurity(FTPClient.SECURITY_FTPES);
     56             client.connect(host, 21);
     57             log.info("连接111.59.53.18 ftp成功!"+client.toString());
     58             client.login(username, password);
     59             log.info("登录ftp成功!当前目录为:"+client.currentDirectory());
     60             File file = new File(zipPath);
     61             if (file.isDirectory()) {
     62                 log.info("我们读的是目录");
     63                 // 列出所有文件名
     64                 String[] files = file.list();
     65                 // 文件数
     66                 int fileNum = files.length;
     67                 log.info("文件夹下有" + fileNum + "个文件");
     68                 if (fileNum > 4) {
     69                     fileNum = 4;
     70                 }
     71                 for (int i = 0; i < fileNum; i++) {
     72                     File file2 = new File(file.getPath() + "\" + files[i]);
     73                     try {
     74 //                        FileInputStream input = new FileInputStream(file2);
     75                         client.upload(file2);
     76 //                        boolean storeFile = client.storeFile(
     77 //                                file2.getName(), input);
     78 //                        input.close();
     79                         // 上传成功后备份压缩文件,不成功可以下次再传
     80 //                        if (storeFile) {
     81                             // 备份
     82                             boolean flag1 = FilesUtil.copyFile2(file2,
     83                                     backPath);
     84 
     85                             if (flag1) {// 备份成功后删除文件;
     86                                 log.info(file2+" 备份成功!");
     87                                 
     88                                 file2.delete();
     89 
     90                             } else {
     91                                 log.error(file2+" 备份成功!");
     92                             }
     93 //                        } else {
     94 //                            logger.error(file2.getName()+" 上传文件失败!");
     95 //                        }
     96                     } catch (Exception e) {
     97                         // TODO Auto-generated catch block
     98                         e.printStackTrace();
     99                     }
    100 
    101                 }
    102                 // Logout
    103                 client.logout();
    104                 
    105                 log.info("退出ftp!");
    106                 // Disconnect
    107 //                ftpClient.disconnect();
    108             }
    109         } catch (Exception e) {
    110             e.printStackTrace();
    111         }
    112     }
    113 }
  • 相关阅读:
    SPSS-Friedman 秩和检验-非参数检验-K个相关样本检验 案例解析
    SPSS-多重响应-频率和交叉表案例分析(问卷调查分析)
    SPSS--回归-多元线性回归模型案例解析
    深入理解RunLoop
    杂七杂八集合
    单元测试
    笔记
    http断点续传
    iOS性能优化
    群聊协议
  • 原文地址:https://www.cnblogs.com/kwzblog/p/7484365.html
Copyright © 2011-2022 走看看