1

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

}
barbsan
  • 3,418
  • 11
  • 21
  • 28
karan babu b
  • 31
  • 10
  • Did you tried to connect via FTP-Tool? – Wulf Apr 01 '19 at 10:49
  • Connection refused is a network level error. It means that a socket could not be opened between your client and the server. use telnet / wireshark / tcpdump to debug your connectivity issues. – Shloim Apr 01 '19 at 11:06
  • @wufo No i didnt tried – karan babu b Apr 01 '19 at 11:25
  • 1
    Try it. If it works, your code is wrong. If it doenst work, your username/passwort is wrong or your ftp-server.... – Wulf Apr 01 '19 at 11:27
  • ok, I will try to use them. – karan babu b Apr 02 '19 at 04:03
  • I tried to debug my program using eclipse. It shows source not found. – karan babu b Apr 02 '19 at 04:38
  • @karanbabub So did you try to connect via a FTP-Tool? – Wulf Apr 02 '19 at 06:57
  • No, before that i tried to using IIS manager in my system and then tried to run my program. Now its showing output : 220 Microsoft FTP Service AUTH TLS 234 AUTH command ok. Expecting TLS Negotiation. Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.net.ftp.FTPSClient.setSocketFactory(Ljavax/net/SocketFactory;)V at org.apache.commons.net.ftp.FTPSClient.disconnect(FTPSClient.java:672) at FTPSs.main(FTPSs.java:93) – karan babu b Apr 02 '19 at 07:04

0 Answers0