M
M
Michael2021-06-24 12:41:24
Java
Michael, 2021-06-24 12:41:24

Java Apache FPTS Client. How to defeat 425 Can't open data connection on storeFile()?

Good day!

I'm trying to upload a file to a remote FTP using Apache FTPSClient:

InputStream inputStream = new FileInputStream("newFile.txt");
        FTPSClient ftpsClient = new FTPSClient();
        try {

            ftpsClient.connect("hostname");
            ftpsClient.login("username", "password");

            int reply = ftpsClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpsClient.disconnect();
            }
            
            boolean changed = ftpsClient.changeWorkingDirectory("files/");
            if (!changed) {
                ftpsClient.disconnect();
            }

            ftpsClient.setFileTransferMode(ftpsClient.BINARY_FILE_TYPE);
            ftpsClient.enterLocalPassiveMode();

            boolean stored = ftpsClient.storeFile("newFile.txt", inputStream);
            if (stored) {
                System.out.println();
            }

            ftpsClient.logout();
            ftpsClient.disconnect();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


Successfully connect , and change directory , but it is not possible to upload the file to remote FTP.
When it comes to storeFile , it returns false, and in the replyString of FTP itself comes: 425 Can't open data connection
There is a file, there is access to remote FTP. Tried uploading via FileZilla and everything works (file upload/delete).

I read that there may be different processing of PASSIVE MODE, and that some FTP servers may not have, or PASSIVE MODE may not work correctly, but the result has not been reached.

Please tell me the options.

PS I also tried to use JSch, Session, but I couldn't even connect (timeout).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-06-24
Hasanly @azerphoenix

Good afternoon.
And tell me please, because it looks like you are trying to connect via FTP, but you are using FTPSClient
https://commons.apache.org/proper/commons-net/apid...
Maybe you need an FTPClient?
https://commons.apache.org/proper/commons-net/apid...
I can assume that you are trying to connect to FTPS using the port for FTP (21) and, accordingly, an error occurs
. For example, when connecting to FTP, I'm ok:

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;

import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

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

        FTPClient ftp = new FTPClient();
        FTPClientConfig config = new FTPClientConfig();
        ftp.configure(config);
        boolean error = false;
        try {
            int reply;
            String host = "example.com";
            ftp.connect(host);
            ftp.login("example_user", "12345");
            System.out.println("Connected to " + host + ".");
            System.out.print(ftp.getReplyString());

            reply = ftp.getReplyCode();

            if(!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                System.err.println("FTP server refused connection.");
                System.exit(1);
            }

            boolean changed = ftp.changeWorkingDirectory("public_html/");
            if (!changed) {
                ftp.disconnect();
            }

            ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            InputStream inputStream = new FileInputStream("/home/admin/Desktop/newFile.txt");
            boolean stored = ftp.storeFile("newFile.txt", inputStream);
            if (stored) {
                System.out.println();
            }

            ftp.logout();
        } catch(IOException e) {
            error = true;
            e.printStackTrace();
        } finally {
            if(ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch(IOException ioe) {
                    // do nothing
                }
            }
            System.exit(error ? 1 : 0);
        }

    }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question