android ftp file tranfer over explicit TLS

后端 未结 3 637
梦毁少年i
梦毁少年i 2021-02-06 17:27

I\'m posting this question after have tried over and over again to get it working, with no success. I trying to implement a FTP file transfer in android, using apache commons li

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 17:54

    I have finally found a solution, the solution was to set the trust manager to accept all certificates. here is the code for those who are experiencing similar problems, maby it can be improved and/or optimized, but it works:

    FTPSClient ftpClient = new FTPSClient("TLS", false);
    try {
        TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
    
            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
    
            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
    
        ftpClient.setTrustManager(trustManager[0]);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(null, null);
        KeyManager km = kmf.getKeyManagers()[0];
        ftpClient.setKeyManager(km);
        ftpClient.setBufferSize(1024 * 1024);
        ftpClient.setConnectTimeout(100000);
        ftpClient.connect(InetAddress.getByName("ipaddress"), 990);
        ftpClient.setSoTimeout(100000);
    
        if (ftpClient.login("user", "password")) {
            ftpClient.execPBSZ(0);
            ftpClient.execPROT("P");
            ftpClient.changeWorkingDirectory("/");
            // 250 = directory succesfully changed
            if (ftpClient.getReplyString().contains("250")) {
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                BufferedInputStream buffIn = null;
    
                for (File picture : pictures) {
                    buffIn = new BufferedInputStream(new FileInputStream(picture.getAbsolutePath()));
                    boolean result = ftpClient.storeFile(picture.getName(), buffIn);
                    try {
                        buffIn.close();
                    } catch (Exception e) {
                    }
                    if (result)
                        picture.delete();
                }
            }
        }
    
    } catch (SocketException e) {
        Log.e("APPTAG", e.getStackTrace().toString());
    } catch (UnknownHostException e) {
        Log.e("APPTAG", e.getStackTrace().toString());
    } catch (IOException e) {
        Log.e("APPTAG", e.getStackTrace().toString());
    } catch (Exception e) {
        Log.e("APPTAG", e.getStackTrace().toString());
    } finally {
        try {
            ftpClient.logout();
        } catch (Exception e2) {
        }
        try {
            ftpClient.disconnect();
        } catch (Exception e2) {
        }
    }
    

提交回复
热议问题