无法使用 FTPSClient 通过 FTP 服务器传输数据 - 连接被拒绝

Not able to transfer data over FTP server using FTPSClient - Connection refused

我正在尝试使用 FTPSClient 连接到 FTP 服务器,虽然身份验证工作正常,但当我尝试发送数据时,我收到 连接被拒绝错误。

package com.main;
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.security.NoSuchAlgorithmException;

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 FTPSExample
{
    public static final void main(String[] args) throws NoSuchAlgorithmException
    {
        int base = 0;
        boolean storeFile = true, binaryTransfer = true, error = false;
        String server, username, password, remote, local;
        String protocol = "SSL";    // SSL/TLS
        FTPSClient ftps;
        
        for (base = 0; base < args.length; base++)
        {
            if (args[base].startsWith("-s"))
                storeFile = true;
            else if (args[base].startsWith("-b"))
                binaryTransfer = true;
            else
                break;
        }

        server = "hostname";
        username = "username";
        password = "pass";
        remote = "/";
        local = "filepath";

        ftps = new FTPSClient(protocol);
       
        ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

        try
        {
            int reply;

            ftps.connect(server);
            System.out.println("Connected to " + server + ".");

            // After connection attempt, you should check the reply code to verify
            // success.
            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 nothing
                }
            }
            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);

            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

}

下面还有生成的日志。

220-FTP Server is ready...
220-WARNING!
220-This information system is the property of ABCD
220-Industries. Access to this resource is limited to
220-authorized use only. Therefore, any or all use of
220-this system may be lawfully intercepted, monitored
220-or audited for reasons including system support,
220-detecting unauthorized use, verifying adherance to
220-security controls or investigating security incidents.
220-By using this system, 1) you accept these terms and
220-2) acknowledge your individual responsibility to
220-comply with all applicable information security
220-policies as a condition of your access. Be assured
220-ABCDIndustries will continue to conduct its
220-information technology practices in accordance
220-with all applicable US and international laws
220-(including privacy laws). A copy of ABCD
220-Industries corporate informaton security policy
220-is located on the ABCD intranet or upon request
220 from your Human Resource representative.
AUTH TLS
234 AUTH command OK. Initializing SSL connection.
Connected to hostname.
USER emerald
331 User name okay, need password.
PASS online
230 User logged in, proceed.
SYST
215 UNIX Type: L8
Remote system is UNIX Type: L8
TYPE I
200 Type set to I.
PASV
227 Entering Passive Mode (xxx,xx,xx,xx,195,80)
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 org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:920)
    at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:627)
    at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
    at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)
    at com.main.FTPSExample.main(FTPSExample.java:126)
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8

注意:我可以从端口 21 上的 WinSCP 连接到服务器,无需加密。

我尝试使用 FTPClient 对象将数据发送到服务器,我从 storeFile 方法得到 true 但我无法在服务器上找到文件,但与 FTPClient 相同的代码正在为其他服务器工作。但是,当我尝试使用 FTPSClient 对象发送数据时,在调用 storeFile 方法时会抛出上述错误。

此外,当我尝试通过在服务器上拖放来在服务器上上传文件时,它会被上传然后自动删除,但不确定为什么会这样。

GUI 日志。

. 2020-03-31 13:23:21.860 --------------------------------------------------------------------------
. 2020-03-31 13:23:21.861 WinSCP Version 5.15.9 (Build 10071) (OS 10.0.18362 - Windows 10 Enterprise)
. 2020-03-31 13:23:21.861 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2020-03-31 13:23:21.861 Log level: Normal
. 2020-03-31 13:23:21.861 Local account: GLOBAL\Devang.Patel
. 2020-03-31 13:23:21.862 Working directory: C:\Program Files (x86)\WinSCP
. 2020-03-31 13:23:21.862 Process ID: 100180
. 2020-03-31 13:23:21.862 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" 
. 2020-03-31 13:23:21.863 Time zone: Current: GMT+5:30 (India Standard Time), No DST
. 2020-03-31 13:23:21.863 Login time: Tuesday, March 31, 2020 1:23:21 PM
. 2020-03-31 13:23:21.863 --------------------------------------------------------------------------
. 2020-03-31 13:23:21.863 Session name: USERNAME@hostname (Site)
. 2020-03-31 13:23:21.863 Host name: hostname (Port: 21)
. 2020-03-31 13:23:21.863 User name: USERNAME (Password: No, Key file: No, Passphrase: No)
. 2020-03-31 13:23:21.863 Transfer Protocol: FTP
. 2020-03-31 13:23:21.863 Ping type: Dummy, Ping interval: 30 sec; Timeout: 15 sec
. 2020-03-31 13:23:21.863 Disable Nagle: No
. 2020-03-31 13:23:21.863 Proxy: None
. 2020-03-31 13:23:21.863 Send buffer: 262144
. 2020-03-31 13:23:21.863 UTF: Auto
. 2020-03-31 13:23:21.863 FTPS: None [Client certificate: No]
. 2020-03-31 13:23:21.863 FTP: Passive: Yes [Force IP: Auto]; MLSD: Auto [List all: Auto]; HOST: Auto
. 2020-03-31 13:23:21.863 Local directory: C:\Users\devang.patel\Desktop, Remote directory: /, Update: Yes, Cache: Yes
. 2020-03-31 13:23:21.864 Cache directory changes: Yes, Permanent: Yes
. 2020-03-31 13:23:21.864 Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
. 2020-03-31 13:23:21.864 Timezone offset: 0h 0m
. 2020-03-31 13:23:21.864 --------------------------------------------------------------------------
. 2020-03-31 13:23:21.935 Connecting to hostname ...
. 2020-03-31 13:23:22.175 Connected with hostname. Waiting for welcome message...
< 2020-03-31 13:23:22.481 220-FTP Server is ready...
< 2020-03-31 13:23:22.481 220-WARNING!
< 2020-03-31 13:23:22.481 220-This information system is the property of ABCD
< 2020-03-31 13:23:22.481 220-Industries. Access to this resource is limited to
< 2020-03-31 13:23:22.481 220-authorized use only. Therefore, any or all use of
< 2020-03-31 13:23:22.481 220-this system may be lawfully intercepted, monitored
< 2020-03-31 13:23:22.481 220-or audited for reasons including system support,
< 2020-03-31 13:23:22.481 220-detecting unauthorized use, verifying adherance to
< 2020-03-31 13:23:22.481 220-security controls or investigating security incidents.
< 2020-03-31 13:23:22.481 220-By using this system, 1) you accept these terms and
< 2020-03-31 13:23:22.482 220-2) acknowledge your individual responsibility to
< 2020-03-31 13:23:22.482 220-comply with all applicable information security
< 2020-03-31 13:23:22.482 220-policies as a condition of your access. Be assured
< 2020-03-31 13:23:22.482 220-ABCD Industries will continue to conduct its
< 2020-03-31 13:23:22.482 220-information technology practices in accordance
< 2020-03-31 13:23:22.482 220-with all applicable US and international laws
< 2020-03-31 13:23:22.482 220-(including privacy laws). A copy of ABCD
< 2020-03-31 13:23:22.482 220-Industries corporate informaton security policy
< 2020-03-31 13:23:22.482 220-is located on the ABCD intranet or upon request
< 2020-03-31 13:23:22.482 220 from your Human Resource representative.
> 2020-03-31 13:23:22.482 USER USERNAME
< 2020-03-31 13:23:22.716 331 User name okay, need password.
> 2020-03-31 13:23:25.742 PASS ******
< 2020-03-31 13:23:25.989 230 User logged in, proceed.
> 2020-03-31 13:23:25.989 SYST
< 2020-03-31 13:23:26.217 215 UNIX Type: L8
> 2020-03-31 13:23:26.217 FEAT
< 2020-03-31 13:23:26.453 211-Extensions supported
< 2020-03-31 13:23:26.453  UTF8
< 2020-03-31 13:23:26.453  OPTS MODE;MLST;UTF8
< 2020-03-31 13:23:26.453  CLNT
< 2020-03-31 13:23:26.453  CSID Name; Version;
< 2020-03-31 13:23:26.453  HOST domain
< 2020-03-31 13:23:26.453  SITE PSWD;SET;ZONE;CHMOD;MSG;EXEC;HELP
< 2020-03-31 13:23:26.453  AUTH TLS;SSL;TLS-C;TLS-P;
< 2020-03-31 13:23:26.453  PBSZ
< 2020-03-31 13:23:26.453  PROT
< 2020-03-31 13:23:26.453  CCC
< 2020-03-31 13:23:26.453  SSCN
< 2020-03-31 13:23:26.453  RMDA directoryname
< 2020-03-31 13:23:26.453  DSIZ
< 2020-03-31 13:23:26.453  AVBL
< 2020-03-31 13:23:26.453  EPRT
< 2020-03-31 13:23:26.453  EPSV
< 2020-03-31 13:23:26.453  MODE Z
< 2020-03-31 13:23:26.453  THMB BMP|JPEG|GIF|TIFF|PNG max_width max_height pathname
< 2020-03-31 13:23:26.453  REST STREAM
< 2020-03-31 13:23:26.453  SIZE
< 2020-03-31 13:23:26.453  MDTM
< 2020-03-31 13:23:26.454  MDTM YYYYMMDDHHMMSS[+-TZ];filename
< 2020-03-31 13:23:26.454  MFMT
< 2020-03-31 13:23:26.454  MFCT
< 2020-03-31 13:23:26.454  MFF Create;Modify;
< 2020-03-31 13:23:26.454  XCRC filename;start;end
< 2020-03-31 13:23:26.454  XMD5 filename;start;end
< 2020-03-31 13:23:26.454  XSHA1 filename;start;end
< 2020-03-31 13:23:26.454  XSHA256 filename;start;end
< 2020-03-31 13:23:26.454  XSHA512 filename;start;end
< 2020-03-31 13:23:26.454  COMB target;source_list
< 2020-03-31 13:23:26.454  MLST Type*;Size*;Create;Modify*;Perm;Win32.ea;Win32.dt;Win32.dl
< 2020-03-31 13:23:26.454 211 End (for details use "HELP commmand" where command is the command of interest)
> 2020-03-31 13:23:26.454 CLNT WinSCP-release-5.15.9
< 2020-03-31 13:23:26.783 200 Noted.
> 2020-03-31 13:23:26.783 OPTS UTF8 ON
< 2020-03-31 13:23:27.195 200 OPTS UTF8 is set to ON.
. 2020-03-31 13:23:27.223 Connected
. 2020-03-31 13:23:27.223 --------------------------------------------------------------------------
. 2020-03-31 13:23:27.223 Using FTP protocol.
. 2020-03-31 13:23:27.224 Doing startup conversation with host.
> 2020-03-31 13:23:27.252 PWD
< 2020-03-31 13:23:27.482 257 "/" is current directory.
. 2020-03-31 13:23:27.482 Changing directory to "/".
> 2020-03-31 13:23:27.483 CWD /
< 2020-03-31 13:23:27.719 250 Directory changed to /
. 2020-03-31 13:23:27.719 Getting current directory name.
> 2020-03-31 13:23:27.720 PWD
< 2020-03-31 13:23:27.950 257 "/" is current directory.
. 2020-03-31 13:23:27.989 Retrieving directory listing...
> 2020-03-31 13:23:27.989 TYPE A
< 2020-03-31 13:23:28.318 200 Type set to A.
> 2020-03-31 13:23:28.318 PASV
< 2020-03-31 13:23:28.625 227 Entering Passive Mode (xxx,xx,xx,xx,195,80)
> 2020-03-31 13:23:28.625 MLSD
. 2020-03-31 13:23:28.625 Connecting to xxx.xx.xx.xx:50000 ...
< 2020-03-31 13:23:28.934 150 Opening BINARY mode data connection for MLSD.
. 2020-03-31 13:23:29.242 Data connection closed
. 2020-03-31 13:23:29.242 Type=dir;Modify=20200330141036.281; etst
< 2020-03-31 13:23:29.242 226 Transfer complete. 42 bytes transferred. 0.04 KB/sec.
. 2020-03-31 13:23:29.243 Directory listing successful
. 2020-03-31 13:23:29.243 ..;D;0;1899-12-30T05:30:00.000Z;0;"" [0];"" [0];---------;0
. 2020-03-31 13:23:29.243 etst;D;0;2020-03-30T14:10:36.000Z;3;"" [0];"" [0];---------;0
. 2020-03-31 13:23:29.263 Startup conversation with host finished.
. 2020-03-31 13:23:59.933 Sending dummy command to keep session alive.
> 2020-03-31 13:23:59.933 REST 0
< 2020-03-31 13:24:00.434 350 Restarting at 0. Send STORE or RETRIEVE.
. 2020-03-31 13:24:29.937 Sending dummy command to keep session alive.
> 2020-03-31 13:24:29.937 REST 0
< 2020-03-31 13:24:30.435 350 Restarting at 0. Send STORE or RETRIEVE.
. 2020-03-31 13:24:33.701 Copying 1 files/directories to remote directory "/" - total size: 104
. 2020-03-31 13:24:33.701   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: Yes; Mask: *.*
. 2020-03-31 13:24:33.701   TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: ; ResumeL: 0
. 2020-03-31 13:24:33.701   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2020-03-31 13:24:33.702 File: 'C:\Users\devang.patel\Desktop\testFtpConnectionFile4293902888177791204.txt' [2020-03-30T07:53:11.476Z] [104]
. 2020-03-31 13:24:33.702 Copying "C:\Users\devang.patel\Desktop\testFtpConnectionFile4293902888177791204.txt" to remote directory started.
. 2020-03-31 13:24:33.703 Binary transfer mode selected.
. 2020-03-31 13:24:33.703 Starting upload of C:\Users\devang.patel\Desktop\testFtpConnectionFile4293902888177791204.txt
> 2020-03-31 13:24:33.703 TYPE I
< 2020-03-31 13:24:33.963 200 Type set to I.
> 2020-03-31 13:24:33.963 PASV
< 2020-03-31 13:24:34.352 227 Entering Passive Mode (xxx,xx,xx,xx,195,81)
> 2020-03-31 13:24:34.352 STOR testFtpConnectionFile4293902888177791204.txt
. 2020-03-31 13:24:34.352 Connecting to xxx.xx.xx.xx:50001 ...
< 2020-03-31 13:24:34.506 150 Opening BINARY mode data connection for testFtpConnectionFile4293902888177791204.txt.
< 2020-03-31 13:24:34.783 226 Transfer complete. 104 bytes transferred. 6.35 KB/sec.
> 2020-03-31 13:24:34.783 MFMT 20200330075311 testFtpConnectionFile4293902888177791204.txt
< 2020-03-31 13:24:35.091 213 Modify=20200330075311; /testFtpConnectionFile4293902888177791204.txt
. 2020-03-31 13:24:35.091 Upload successful
. 2020-03-31 13:24:35.091 Transfer done: 'C:\Users\devang.patel\Desktop\testFtpConnectionFile4293902888177791204.txt' => '/testFtpConnectionFile4293902888177791204.txt' [104]
. 2020-03-31 13:24:35.099 Copying finished: Transferred: 104, Elapsed: 0:00:01, CPS: 0/s
. 2020-03-31 13:24:35.100 Retrieving directory listing...
> 2020-03-31 13:24:35.100 TYPE A
< 2020-03-31 13:24:35.398 200 Type set to A.
> 2020-03-31 13:24:35.398 PASV
< 2020-03-31 13:24:35.705 227 Entering Passive Mode (xxx,xx,xx,xx,195,82)
> 2020-03-31 13:24:35.705 MLSD
. 2020-03-31 13:24:35.706 Connecting to xxx.xx.xx.xx:50002 ...
< 2020-03-31 13:24:36.022 150 Opening BINARY mode data connection for MLSD.
< 2020-03-31 13:24:36.320 226 Transfer complete. 134 bytes transferred. 0.13 KB/sec.
. 2020-03-31 13:24:36.321 Data connection closed
. 2020-03-31 13:24:36.321 Type=dir;Modify=20200330141036.281; etst
. 2020-03-31 13:24:36.321 Type=file;Size=104;Modify=20200330075311.000; testFtpConnectionFile4293902888177791204.txt
. 2020-03-31 13:24:36.321 Directory listing successful
. 2020-03-31 13:24:36.322 ..;D;0;1899-12-30T05:30:00.000Z;0;"" [0];"" [0];---------;0
. 2020-03-31 13:24:36.322 etst;D;0;2020-03-30T14:10:36.000Z;3;"" [0];"" [0];---------;0
. 2020-03-31 13:24:36.322 testFtpConnectionFile4293902888177791204.txt;-;104;2020-03-30T07:53:11.000Z;3;"" [0];"" [0];---------;0
. 2020-03-31 13:25:06.935 Sending dummy command to keep session alive.
> 2020-03-31 13:25:06.936 TYPE A
< 2020-03-31 13:25:07.437 200 Type set to A.
. 2020-03-31 13:25:36.941 Sending dummy command to keep session alive.
> 2020-03-31 13:25:36.941 TYPE I
< 2020-03-31 13:25:37.441 200 Type set to I.
. 2020-03-31 13:26:06.944 Sending dummy command to keep session alive.
> 2020-03-31 13:26:06.944 PWD
< 2020-03-31 13:26:07.444 257 "/" is current directory.
. 2020-03-31 13:27:08.946 Sending dummy command to keep session alive.
> 2020-03-31 13:27:08.946 REST 0
< 2020-03-31 13:27:09.447 350 Restarting at 0. Send STORE or RETRIEVE.
. 2020-03-31 13:27:29.907 Getting current directory name.
. 2020-03-31 13:27:29.909 Retrieving directory listing...
> 2020-03-31 13:27:29.909 TYPE A
< 2020-03-31 13:27:30.140 200 Type set to A.
> 2020-03-31 13:27:30.140 PASV
< 2020-03-31 13:27:30.413 227 Entering Passive Mode (xxx,xx,xx,xx,195,83)
> 2020-03-31 13:27:30.413 MLSD
. 2020-03-31 13:27:30.413 Connecting to xxx.xx.xx.xx:50003 ...
< 2020-03-31 13:27:30.657 150 Opening BINARY mode data connection for MLSD.
< 2020-03-31 13:27:30.927 226 Transfer complete. 42 bytes transferred. 0.04 KB/sec.
. 2020-03-31 13:27:30.927 Data connection closed
. 2020-03-31 13:27:30.928 Type=dir;Modify=20200330141036.281; etst
. 2020-03-31 13:27:30.928 Directory listing successful
. 2020-03-31 13:27:30.928 ..;D;0;1899-12-30T05:30:00.000Z;0;"" [0];"" [0];---------;0
. 2020-03-31 13:27:30.928 etst;D;0;2020-03-30T14:10:36.000Z;3;"" [0];"" [0];---------;0
. 2020-03-31 13:29:00.953 Sending dummy command to keep session alive.
> 2020-03-31 13:29:00.953 TYPE A
< 2020-03-31 13:29:01.455 200 Type set to A.

不是你代码的问题。

如您所见,启用加密后,即使使用 GUI FTP 客户端 (WinSCP) 也无法连接。

这表明您遇到了防火墙问题。
参见 File transfer with vsftpd does not work with TLS, but does work with unencrypted connection