无法连接到服务器。 java.net.ConnectException:连接被拒绝:connect

时间:2019-04-01 10:41:56

标签: java ftps

我想使用FTPS协议发送到服务器,并尝试使用Java程序连接到服务器。我创建了一个代码,使用FTPSClient连接服务器,以使用服务器的INetAddressPort号从服务器发送和接收文件。当我运行代码时,它显示以下错误。

  

无法连接到服务器。

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)

下面是我的Java程序,该程序使用FTPSClient连接到文件传输。

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

}

0 个答案:

没有答案