Search code examples
javaftp-clientftps

FTPS storeFile return always false Java


im trying to send files to FTPS server connection method: FTPS, ACTIVE, EXPLICIT

setFileType(FTP.BINARY_FILE_TYPE);
setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);

Checking the reply string right after connect i got:

234 AUTH command ok. Expecting TLS Negotiation.

from here

 234    Specifies that the server accepts the authentication mechanism specified by the client, and the exchange of security data is complete. A higher level nonstandard code created by Microsoft.

while trying to send file with storeFile or storeUniqeFile i get false

checking the reply string right after store file i got: 501 Server cannot accept argument.

what is weird i was able creating a directory to this client without any issues with makeDirectory("test1");

i was trying both this links : link1 , link2

FOR EXAMPLE when i was trying to use ftp.enterLocalPassiveMode(); before ftp.storeFile(destinationfile, in); i got time out error .

Does anyone have any idea how to solve it ?

public static void main(String[] args) throws Exception {

    FTPSProvider ftps = new FTPSProvider();
    String json =    "connection details";
    DeliveryDetailsFTPS details = gson.fromJson(json, DeliveryDetailsFTPS .class);
    File file = File.createTempFile("test", ".txt");
    FileUtils.write(file, " some test", true);
    try (FileInputStream stream = new FileInputStream(file)) {
        ftps.sendInternal(ftps.getClient(details), details, stream, file.getName());
    }
}




protected void sendInternal(FTPClient client, DeliveryDetailsFTPS  details, InputStream stream, String filename) throws Exception {

    try {
        // release the enc
        DeliveryDetailsFTPS ftpDetails = (DeliveryDetailsFTPS) details;
        setClient(client, ftpDetails);
        boolean isSaved = false;
        try (BufferedInputStream bis = new BufferedInputStream(stream)) {
            isSaved = client.storeFile(filename, bis);
        }
        client.makeDirectory("test1");
        client.logout();
        if (!isSaved) {
            throw new IOException("Unable to upload file to FTP");
        }
    } catch (Exception ex) {
        LOG.debug("Unable to send to FTP", ex);
        throw ex;
    } finally {
        client.disconnect();
    }
}




@Override
protected FTPClient getClient(DeliveryDetails details) {
    return new FTPSClient(isImplicitSSL((DeliveryDetailsFTPS ) details));
}

    protected void setClient(FTPClient client, DeliveryDetailsFTPS details) throws Exception {
        DeliveryDetailsFTPS ftpDetails = (DeliveryDetailsFTPS ) details;
        client.setConnectTimeout(100000);
        client.setDefaultTimeout(10000 * 60 * 2);
         
            client.setControlKeepAliveReplyTimeout(300);
         
              client.setControlKeepAliveTimeout(300);
     
        client.setDataTimeout(15000);
        client.connect(ftpDetails.host, ftpDetails.port);
        client.setBufferSize(1024 * 1024);
        client.login(ftpDetails.username, ftpDetails.getSensitiveData());
        client.setControlEncoding("UTF-8");

        int code = client.getReplyCode();
        if (code == 530) {
            throw new IOException(client.getReplyString());
        }

        // Set binary file transfer
        client.setFileType(FTP.BINARY_FILE_TYPE);
        client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);

        if (ftpDetails.ftpMode == FtpMode.PASSIVE) {
            client.enterLocalPassiveMode();
        }

         client.changeWorkingDirectory(ftpDetails.path);
    }

I have tried this solution as well didn't solve the problem:

they only way i was able send file is with FileZilla and it is using FTPES . But i need my Java code to do it . can anyone give me a clue


Solution

  • I have tried almost any possible solution offered on different websites could not make it work with Apache FTPS CLIENT , had to use a different class which worked like a charm here is a snippet:

    com.jscape.inet.ftps Link

    private Ftps sendWithFtpsJSCAPE(ConnDetails details, InputStream stream, String filename) throws FtpException, IOException {
        Ftps ftp;
        FtpConnectionDetails ftpDetails = FtpConnectionDetails details;
    
        ftp = new Ftps(ftpDetails.getHost(), ftpDetails.getUsername(), ftpDetails.getPassword());
    
    
    
        if (ftpDetails.getSecurityMode().equals(FtpConnectionDetails.SecurityMode.EXPLICIT)) {
            ftp.setConnectionType(Ftps.AUTH_TLS);
        } else {
            ftp.setConnectionType(Ftps.IMPLICIT_SSL);
        }
        ftp.setPort(ftpDetails.getPort());
    
        if (!ftpDetails.getFtpMode().equals(FtpMode.ACTIVE)) {
            ftp.setPassive(true);
        }
        ftp.setTimeout(FTPS_JSCAPE_TIME_OUT);
        ftp.connect();
        ftp.setBinary();
        ftp.setDir(ftpDetails.getPath());
        ftp.upload(stream, filename);
    
        return ftp;
    }