I want to send to server using FTPS protocol and tried to connect to server using java program. I create a code to connect server using FTPSClient for sending and receiving files from server using its INetAddress and Port number. When I run my code it shows the below error.
Could not connect to server.
java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.<init>(Unknown Source) at java.net.Socket.<init>(Unknown Source) at org.apache.commons.net.DefaultSocketFactory.createSocket(DefaultSocketFactory.java:67) at org.apache.commons.net.SocketClient.connect(SocketClient.java:141) at sam.FTPSs.main(FTPSs.java:79)
below is my java program to connect using FTPSClient to file transfer.
package sam;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
public final class FTPSs
{
    public static final String USAGE =
        "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
        "\n Default behavior is to download a file and use ASCII transfer mode.\n" +
        "\t-s store file on server (upload)\n" +
        "\t-b use binary transfer mode\n";
    @SuppressWarnings({ "resource", "unused" })
    public static final void main(String[] args) throws NoSuchAlgorithmException, UnknownHostException
    {
        int base = 0;
        boolean storeFile = false, binaryTransfer = false, error = false;
        String username, password, remote, local;
        String protocol = "SSL";    // SSL/TLS
        FTPSClient ftps;
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        if (n==1)
            storeFile = true;
        else if (n==2)
            binaryTransfer = true;
        InetAddress server =  InetAddress.getByName("172.22.126.172");//"172.22.106.181";//
        username = "karan-pt2843";
        password = "Nivedhaav@1";
        remote = "D:/content.txt";
       local = "D:/sam.txt";
        ftps = new FTPSClient(protocol);       
        ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        try
        {//ftps.enterRemoteActiveMode(server, 21);
            int reply;
            ftps.connect(server, 21);
            System.out.println("Connected to " + server + ".");
            reply = ftps.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftps.disconnect();
                System.err.println("FTP server refused connection.");
                System.exit(1);
            }
        }
        catch (IOException e)
        {
            if (ftps.isConnected())
            {
                try
                {
                    ftps.disconnect();
                }
                catch (IOException f)
                {
                    // do nothin
                }
            }
            System.err.println("Could not connect to server.");
            e.printStackTrace();
            System.exit(1);
        }
__main:
        try
        {
            ftps.setBufferSize(1000);
            if (!ftps.login(username, password))
            {
                ftps.logout();
                error = true;
                break __main;
            }
            System.out.println("Remote system is " + ftps.getSystemName());
            if (binaryTransfer) ftps.setFileType(FTP.BINARY_FILE_TYPE);
            // Use passive mode as default because most of us are
            // behind firewalls these days.
            ftps.enterLocalPassiveMode();
            if (storeFile)
            {
                InputStream input;
                input = new FileInputStream(local);
                ftps.storeFile(remote, input);
                input.close();
            }
            else
            {
                OutputStream output;
                output = new FileOutputStream(local);
                ftps.retrieveFile(remote, output);
                output.close();
            }
            ftps.logout();
        }
        catch (FTPConnectionClosedException e)
        {
            error = true;
            System.err.println("Server closed connection.");
            e.printStackTrace();
        }
        catch (IOException e)
        {
            error = true;
            e.printStackTrace();
        }
        finally
        {
            if (ftps.isConnected())
            {
                try
                {
                    ftps.disconnect();
                }
                catch (IOException f)
                {
                    // do nothing
                }
            }
        }
        System.exit(error ? 1 : 0);
    } // end main
}